Confusion about await
I just recently understood: The CO module implements the automatic calling of yield in the generator.
But if it is called automatically, where are the resolve and reject callback methods of the promise object behind each yield defined?
Today I saw that ES7’s async replaced function*, await replaced yield,
I have the same confusion again. Where are the resolve and reject callback methods of the promise object behind each await defined?
var fs = require('fs');
var readFile = function (fileName) {
return new Promise(function (resolve, reject) {
fs.readFile(fileName, function(error, data) {
if (error) reject(error);
resolve(data);
});
});
};
var asyncReadFile = async function (
) {
var f1 = await readFile('/etc/fstab');//这里没有定义回调,回调在哪里定义
var f2 = await readFile('/etc/shells');//这里没有定义回调,回调在哪里定义
console.log(f1.toString());
console.log(f2.toString());
};
f1 f2 is the return of resolve.
If you want to handle the return of reject, please use try catch