Skip to content

Commit 2f23486

Browse files
authored
Merge pull request lydiahallie#565 from vzaidman/patch-1
Improved 133 example and explanation
2 parents 684a18b + 4a2aa8f commit 2f23486

File tree

1 file changed

+21
-17
lines changed

1 file changed

+21
-17
lines changed

README.md

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4277,44 +4277,48 @@ We invoke `counterTwo.increment()`, which sets `count` to `3`. Then, we log the
42774277
###### 133. What's the output?
42784278
42794279
```javascript
4280-
const myPromise = Promise.resolve(Promise.resolve('Promise!'));
4280+
const myPromise = Promise.resolve(Promise.resolve('Promise'));
42814281
42824282
function funcOne() {
4283-
myPromise.then(res => res).then(res => console.log(res));
4284-
setTimeout(() => console.log('Timeout!'), 0);
4285-
console.log('Last line!');
4283+
setTimeout(() => console.log('Timeout 1!'), 0);
4284+
myPromise.then(res => res).then(res => console.log(`${res} 1!`));
4285+
console.log('Last line 1!');
42864286
}
42874287
42884288
async function funcTwo() {
42894289
const res = await myPromise;
4290-
console.log(await res);
4291-
setTimeout(() => console.log('Timeout!'), 0);
4292-
console.log('Last line!');
4290+
console.log(`${res} 2!`)
4291+
setTimeout(() => console.log('Timeout 2!'), 0);
4292+
console.log('Last line 2!');
42934293
}
42944294
42954295
funcOne();
42964296
funcTwo();
42974297
```
42984298
4299-
- 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!`
43034303
43044304
<details><summary><b>Answer</b></summary>
43054305
<p>
43064306
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.
43084312
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's busy completing the promise and handling the `setTimeout` callback. This means that `Last line 1!` gets logged first, since this is not an asynchonous operation.
43104314

4311-
Both the promise and the timeout are asynchronous operations, the function keeps on running while it's busy completing the promise and handling the `setTimeout` callback. This means that `Last line!` gets logged first, since this is not an asynchonous operation. This is the last line of `funcOne`, the promise resolved, and `Promise!` gets logged. However, since we're invoking `funcTwo()`, the call stack isn't empty, and the callback of the `setTimeout` function cannot get added to the callstack yet.
4315+
Since the callstack is not empty yet, the `setTimeout` function and promise in `funcOne` cannot get added to the callstack yet.
43124316

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 (or rejected). 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.
43144318

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.
43164320

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.
43184322

43194323
</p>
43204324
</details>

0 commit comments

Comments
 (0)