# What 'Code' is executed at what time?

**URL:** https://community.thunkable.com/t/what-code-is-executed-at-what-time/1042381
**Category:** Questions about Thunkable X
**Created:** [January 8, 2021, 4:54pm UTC](https://community.thunkable.com/t/what-code-is-executed-at-what-time/1042381 "2021-01-08T16:54:53Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![tmurachpx](https://avatars.discourse-cdn.com/v4/letter/t/c68b51/32.png) [@tmurachpx](https://community.thunkable.com/u/tmurachpx)
#### Post date: [January 8, 2021, 4:54pm UTC](https://community.thunkable.com/t/what-code-is-executed-at-what-time/1042381/1 "2021-01-08T16:54:54Z")

</div>

Hey Guys,  
I have some trouble understanding how the ‘timeline’ or execution of blocks works.  
My precise problem is that I use one Button which has 2 functions inside.

1. function: upload some images to cloudinary and get their urls
2. function: create a new object with some info and these image urls and upload them into a database (webapi)

Now, I noticed that sometimes the urls from function 1 make it into the database in function 2 and sometimes not. I added some random points in the code where it waits for a second or two. That increased the chance of landing in the database but it does not always happen.

So in general I thought one block is executed after the one before. So function 1 should finish before function 2 right? Is that that always the case? If no, when not and where can I learn about it?

A second question regarding this: if I WANT things to happen simultaniously, how do I do this? eg I want a sound recording of 5 seconds. (the recording block lets my do this). But I also want a timer to run for 5 seconds at the same time to show the user how much time he has left. So far my timer shows the 5 seconds and THEN the recording starts 😅

A bit lost on the topic. Any source or info would help 😉

cheers!

---

<div class="post-metadata">

### Author: ![drted](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/drted/32/92694_2.png) [@drted](https://community.thunkable.com/u/drted)
#### Post date: [January 8, 2021, 5:04pm UTC](https://community.thunkable.com/t/what-code-is-executed-at-what-time/1042381/2 "2021-01-08T17:04:02Z")

</div>

Check out this discussion on asynchronous processing in Thunkable.

> [@Wrapping Asynchronous (then do) blocks into a Synchronous Function resolves unpredictable results](http://community.thunkable.com/t/wrapping-asynchronous-then-do-blocks-into-a-synchronous-function-resolves-unpredictable-results/910511):
>
> Thunkable provides an extensive library of calls to external services including Google Docs and Web APIs. These external calls are, by necessity, asynchronous calls. That is, the request is sent to the external serivce, Thunkable block execution continues on it’s merry way, then the external service sends a reply. Thunkable wraps the external calls in in blocks with “Then Do” sections. This is a massive improvement over separate call and response block (MIT App Inventor), but it is not without …

---

<div class="post-metadata">

### Author: ![tmurachpx](https://avatars.discourse-cdn.com/v4/letter/t/c68b51/32.png) [@tmurachpx](https://community.thunkable.com/u/tmurachpx)
#### Post date: [January 8, 2021, 5:21pm UTC](https://community.thunkable.com/t/what-code-is-executed-at-what-time/1042381/3 "2021-01-08T17:21:27Z")

</div>

ah man, that is genius! Exactly what I need to know. Thanks alot!  
Do you also have any idea for the second question: How to execute some blocks at the same time?

---

<div class="post-metadata">

### Author: ![drted](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/drted/32/92694_2.png) [@drted](https://community.thunkable.com/u/drted)
#### Post date: [January 8, 2021, 5:23pm UTC](https://community.thunkable.com/t/what-code-is-executed-at-what-time/1042381/4 "2021-01-08T17:23:56Z")

</div>

I believe @tatiang did something with sound and timers.

---

<div class="post-metadata">

### Author: ![tatiang](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/tatiang/32/55482_2.png) [@tatiang](https://community.thunkable.com/u/tatiang)
#### Post date: [January 8, 2021, 5:34pm UTC](https://community.thunkable.com/t/what-code-is-executed-at-what-time/1042381/5 "2021-01-08T17:34:30Z")

</div>

Did I? I probably did! I have so many demos lying around.

I’m not recalling that but executing blocks at the same time… you’d just have two event blocks.

So instead of this:

```auto
On button click
     Play sound
     Start timer

```

You would have this:

```auto
On button click
     Play sound

On button click 
     Start timer

```

---

<div class="post-metadata">

### Author: ![drted](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/drted/32/92694_2.png) [@drted](https://community.thunkable.com/u/drted)
#### Post date: [January 8, 2021, 5:39pm UTC](https://community.thunkable.com/t/what-code-is-executed-at-what-time/1042381/6 "2021-01-08T17:39:12Z")

</div>

I was thing of this post

> [@Looping sound w/pause](http://community.thunkable.com/t/looping-sound-w-pause/1035786/9):
>
> Here are the blocks that allowed me to loop, pause, and resume a sound: [image]

---

<div class="post-metadata">

### Author: ![tmurachpx](https://avatars.discourse-cdn.com/v4/letter/t/c68b51/32.png) [@tmurachpx](https://community.thunkable.com/u/tmurachpx)
#### Post date: [January 8, 2021, 6:26pm UTC](https://community.thunkable.com/t/what-code-is-executed-at-what-time/1042381/7 "2021-01-08T18:26:31Z")

</div>

ah the problem is this:  
I record a sound and the recorder block has a function “record for x seconds”. So you start it, it records for X seconds and stops. No matter which way you turn it: either the timer or the recording rund for X seconds before the other one start.

I have an idea how to solve this, as there is a different recorder block which just starts the recording. I can then run the timer and then stop the recording.

I was just curious if there was a general way how to start multiple tasks at the same time 😉

thanks a lot for your time and help!!

---

<div class="post-metadata">

### Author: ![tatiang](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/tatiang/32/55482_2.png) [@tatiang](https://community.thunkable.com/u/tatiang)
#### Post date: [January 8, 2021, 6:38pm UTC](https://community.thunkable.com/t/what-code-is-executed-at-what-time/1042381/8 "2021-01-08T18:38:14Z")

</div>

Don’t use the built-in timer. Just use the “seconds since 1970” block in the Device drawer. You can set a variable to that block just before you start recording. Then, after the recording is done, compare the time by setting a different variable to seconds since 1970 minus the first variable. That will give you the amount of time that has elapsed.

---

<div class="post-metadata">

### Author: ![ioannis](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/ioannis/32/146956_2.png) [@ioannis](https://community.thunkable.com/u/ioannis)
#### Post date: [November 8, 2024, 1:30pm UTC](https://community.thunkable.com/t/what-code-is-executed-at-what-time/1042381/9 "2024-11-08T13:30:58Z")

</div>


