You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
- A: `Promise! Last line! Promise! Last line! Last line! Promise!`
4300
-
- B: `Last line! Timeout! Promise! Last line! Timeout! Promise!`
4301
-
- C: `Promise! Last line! Last line! Promise! Timeout! Timeout!`
4302
-
- D: `Last line! Promise! Promise! Last line! Timeout! Timeout!`
4299
+
- A: `Promise 1! Last line 1! Promise 2! Last line 2! Timeout 1! Timeout 2!`
4300
+
- B: `Last line 1! Timeout 1! Promise 1! Last line 2! Promise2! Timeout 2! `
4301
+
- C: `Last line 1! Promise 2! Last line 2! Promise 1! Timeout 1! Timeout 2!`
4302
+
- D: `Timeout 1! Promise 1! Last line 1! Promise 2! Timeout 2! Last line 2!`
4303
4303
4304
4304
<details><summary><b>Answer</b></summary>
4305
4305
<p>
4306
4306
4307
-
#### Answer: D
4307
+
#### Answer: C
4308
+
4309
+
First, we invoke `funcOne`. On the first line of `funcOne`, we call the _asynchronous_ `setTimeout` function, from which the callback is sent to the Web API. (see my article on the event loop <a href="https://dev.to/lydiahallie/javascript-visualized-event-loop-3dif">here</a>.)
4310
+
4311
+
Then we call the `myPromise` promise, which is an _asynchronous_ operation.
4308
4312
4309
-
First, we invoke `funcOne`. On the first line of `funcOne`, we call the `myPromise` promise, which is an _asynchronous_ operation. While the engine is busy completing the promise, it keeps on running the function `funcOne`. The next line is the _asynchronous_ `setTimeout` function, from which the callback is sent to the Web API. (see my article on the event loop <a href="https://dev.to/lydiahallie/javascript-visualized-event-loop-3dif">here</a>.)
4313
+
Both the promise and the timeout are asynchronous operations, the function keeps on running while it'sbusycompletingthepromiseandhandlingthe`setTimeout`callback. Thismeansthat`Last line 1!`getsloggedfirst, sincethisisnotanasynchonousoperation.
4310
4314
4311
-
Both the promise and the timeout are asynchronous operations, the function keeps on running while it'sbusycompletingthepromiseandhandlingthe`setTimeout`callback. Thismeansthat`Last line!`getsloggedfirst, sincethisisnotanasynchonousoperation. Thisisthelastlineof`funcOne`, thepromiseresolved, and`Promise!`getslogged. However, sincewe're invoking `funcTwo()`, the call stack isn'tempty, andthecallbackofthe`setTimeout`function cannot get added to the callstack yet.
4315
+
Sincethecallstackisnotemptyyet, the`setTimeout`functionand promise in `funcOne` cannot get added to the callstack yet.
4312
4316
4313
-
In `funcTwo` we're, first _awaiting_ the myPromise promise. With the `await` keyword, we pause the execution of the function until the promise has resolved (orrejected). Then, we log the awaited value of `res` (since the promise itself returns a promise). This logs `Promise!`.
4317
+
In `funcTwo`, the variable `res` gets `Promise` because `Promise.resolve(Promise.resolve('Promise'))` is equivalent to `Promise.resolve('Promise')` since resolving a promise just resolves it's value. The `await` in this line stops the execution of the function until it receives the resolution of the promise and then keeps on running synchronously until completion, so `Promise 2!` and then `Last line 2!` are logged and the `setTimeout` is sent to the Web API.
4314
4318
4315
-
The next line is the _asynchronous_ `setTimeout` function, from which the callback is sent to the Web API.
4319
+
Then the call stack is empty. Promises are _microtasks_ so they are resolved first when the call stack is empty so `Promise 1!` gets to be logged.
4316
4320
4317
-
We get to the last line of `funcTwo`, which logs `Last line!` to the console. Now, since `funcTwo` popped off the call stack, the call stack is empty. The callbacks waiting in the queue (`() => console.log("Timeout!")` from `funcOne`, and `() => console.log("Timeout!")` from `funcTwo`) get added to the call stack one by one. The first callback logs `Timeout!`, and gets popped off the stack. Then, the second callback logs `Timeout!`, and gets popped off the stack. This logs `Last line! Promise! Promise! Last line! Timeout! Timeout!`
4321
+
Now, since `funcTwo` popped off the call stack, the call stack is empty. The callbacks waiting in the queue (`() => console.log("Timeout 1!")` from `funcOne`, and `() => console.log("Timeout 2!")` from `funcTwo`) get added to the call stack one by one. The first callback logs `Timeout 1!`, and gets popped off the stack. Then, the second callback logs `Timeout 2!`, and gets popped off the stack.
0 commit comments