Skip to content

Commit 80d3916

Browse files
committed
Fix question 133
1 parent 570f2df commit 80d3916

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4277,14 +4277,14 @@ const myPromise = Promise.resolve(Promise.resolve('Promise!'));
42774277
42784278
function funcOne() {
42794279
myPromise.then(res => res).then(res => console.log(res));
4280-
setTimeout(() => console.log('Timeout!', 0));
4280+
setTimeout(() => console.log('Timeout!'), 0);
42814281
console.log('Last line!');
42824282
}
42834283
42844284
async function funcTwo() {
42854285
const res = await myPromise;
42864286
console.log(await res);
4287-
setTimeout(() => console.log('Timeout!', 0));
4287+
setTimeout(() => console.log('Timeout!'), 0);
42884288
console.log('Last line!');
42894289
}
42904290
@@ -4302,7 +4302,7 @@ funcTwo();
43024302
43034303
#### Answer: D
43044304
4305-
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 here.)
4305+
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>.)
43064306
43074307
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.
43084308

0 commit comments

Comments
 (0)