mirror of
https://github.com/xfarrow/javascript-tutorials.git
synced 2025-02-11 01:00:51 +01:00
Compare commits
2 Commits
374f29c955
...
7b10c3ce19
Author | SHA1 | Date | |
---|---|---|---|
|
7b10c3ce19 | ||
|
04ab3177d2 |
@ -23,14 +23,13 @@ async function f2 () {
|
|||||||
f2()
|
f2()
|
||||||
|
|
||||||
// The code in the same function after "await"
|
// The code in the same function after "await"
|
||||||
// is to be intended in the "then()" of the primise. This means
|
// is to be intended in the "then()" of the promise.
|
||||||
// that after await (but before the completion of the Promise),
|
// Consider the following example:
|
||||||
// the flow of execution goes out that code block. For example
|
|
||||||
// consider the following example:
|
|
||||||
async function exampleAsyncFunction () {
|
async function exampleAsyncFunction () {
|
||||||
console.log('Before await')
|
console.log('Before await')
|
||||||
await new Promise(function (resolve, reject) {
|
await new Promise(function (resolve, reject) {
|
||||||
setTimeout(() => resolve('done'), 500)
|
console.log("In the Promise")
|
||||||
|
setTimeout(() => resolve(), 500)
|
||||||
}) // Pauses execution here until the promise resolves.
|
}) // Pauses execution here until the promise resolves.
|
||||||
console.log('After await')
|
console.log('After await')
|
||||||
}
|
}
|
||||||
@ -38,11 +37,52 @@ console.log('Start')
|
|||||||
exampleAsyncFunction()
|
exampleAsyncFunction()
|
||||||
console.log('End')
|
console.log('End')
|
||||||
|
|
||||||
// The result will be:
|
/*
|
||||||
// Start, Before Await, End, After await
|
|
||||||
// "End" comes before "After Await" because the
|
The result will be:
|
||||||
// flow of execution goes to the caller
|
Start, Before Await, In the Promise, End, After await
|
||||||
// when await is invoked.
|
"End" comes before "After Await" because the
|
||||||
|
flow of execution goes to the caller of exampleAsyncFunction
|
||||||
|
after setTimeout() gets enqueued in the Event Loop Thread
|
||||||
|
but before 500ms have elapsed.
|
||||||
|
|
||||||
|
+---------------+
|
||||||
|
| Main |
|
||||||
|
+---------------+
|
||||||
|
|
|
||||||
|
|
|
||||||
|
v
|
||||||
|
+----------------------+
|
||||||
|
|exampleAsyncFunction |
|
||||||
|
+----------------------+
|
||||||
|
|
|
||||||
|
|
|
||||||
|
v
|
||||||
|
+---------------+
|
||||||
|
| Promise |
|
||||||
|
+---------------+
|
||||||
|
|
|
||||||
|
|
|
||||||
|
v
|
||||||
|
+----------------------------------------+
|
||||||
|
|Enqueue resolve() and exit immediately | +--------------------------------+
|
||||||
|
+----------------------------------------+ -------------------------------> |Wait 500ms in the libuv library |
|
||||||
|
| +--------------------------------+
|
||||||
|
| Nothing left to do in the AWAIT, return to caller |
|
||||||
|
v |
|
||||||
|
+---------------+ |
|
||||||
|
| Main | |
|
||||||
|
+---------------+ |
|
||||||
|
| |
|
||||||
|
| |
|
||||||
|
v |
|
||||||
|
+-------------------+ |
|
||||||
|
| Then() of Promise | <-----------------------------------------------------------
|
||||||
|
+-------------------+
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
// Questions
|
// Questions
|
||||||
//
|
//
|
||||||
|
Loading…
x
Reference in New Issue
Block a user