Week 2 : Adding Promises to setTimeout
For this week I added promises to setTimeout in the block.js file.The reason for adding promises is to prevent race condition.
setTimeout and Promises both serve to execute code in an asynchronous way but with differences characteristics and purposes:
setTimeout — Delays the execution of a function by specific time duration.Does not block the rest of the code execution (asynchronous behavior) They create Macro task (browser internal operation)
Promises — They are a wrapper to allow asynchronous execution of code (Does not depend on specific time duration)They are especially useful for changing different async calls. Does not block the rest of the code execution (asynchronous behavior) it uses the await operator. They create Micro task (browser internal operation), which have priority over the Macro task.
Promises work by hooking on the return values of functions. A function returns a promise which in turn you can hook on. setTimeout introduces a timeout, it's quite possible that A will not finish within the timeout and B will start running before A is finished with nested callbacks. The only difference is that it might wait long enough this is called a race condition
Profiling Result on Musicblocks
I used one of examples on Musicblocks to get this particular profiling results