Async/Await
To better understand the behavior of async and await keywords in JavaScript, I have written the below code and check the result.
1 | const fs = require('fs'); |
Result:
1 | $ node app.js |
Here are the processing steps for the above example.
- The runtime engine will call main function
asyncMain
, which will in turn call the firstawait
async functionasyncReadfile1
, which will output console logThe await async function to read file 1 is called
- When the function
asyncReadfile1
runs to the statementconst text = await readFile("C:/Download/129.xps");
, the Promise object returned fromreadFile
is not resolved yet, so the execution stack will go back to upper functionasyncMain
. In the main function, the Promise object returned fromasyncReadfile1
is not resolved yet, so the executation stack will go back to upper level. Therefore, the next console log isGo to the next statement after the async main function
. - When the sync processing logic in the upmost level is completed, the call stack goes back to deepest unresolved Promise function
asyncReadfile1
. In the async functionasyncReadfile1
, the statementconst text = await readFile("C:/Download/129.xps");
is completed and the Promise object is resolved, so it will continue with the next statement, and the next console log isThe file 1 contains: 251610 bytes
. After the console log, the Promise object of async functionasyncReadfile1
is resolved. - Since the promise object of async function
asyncReadfile1
is resolved, the main function will continue with next statement, which will output console logProcessing between reading 2 files
. - In the next statement of main function, async function
asyncReadfile2
wil be called, and the console log will beThe await async function to read file 2 is called
andThe file 2 contains: 549337 bytes
- After the return statement of main function, the Promise object of the main function is resolved, so the callback function in
Promise.prototype.then
method will be executed to output console logThe async main function is called successful
Understanding for async/await:
- When the Promise object returned from
await
child function is not resolved yet, the execution stack will go back to parent function. - When the Promise object returned from
await
child function is resolved, the execution stack will continue with next statement in the current function. - When the sync processing logic in the upmost level is completed, the call stack goes back to deepest unresolved Promise function.
Promise
To achieve the same functionality with Promise object, we would need to more complex code with nested Promise.prototype.then
method.
1 | const fs = require('fs'); |
Understanding for Promise
The resolved immediately Promise object will be effective in next Event loop, instead of current Event loop.
1
2
3
4
5
6
7
8
9
10
11
12
13setTimeout(function () {
console.log('three');
}, 0);
Promise.resolve().then(function () {
console.log('two');
});
console.log('one');
// one
// two
// threeThe
Promise.prototype.then
method will return another Promise object, which is not the original Promise object.- The new Promise object will be executed immediately after creation.
1
2
3
4
5
6
7
8
9
10
11
12var promise = new Promise(function(resolve, reject){
console.log("New Promise object");
resolve();
}).then( () => {
console.log("Callback method");
});
console.log("After creating new Promise object");
// New Promise object
// After creating new Promise object
// Callback method