javascript - Where to write the callback method of the Promise object behind await
大家讲道理
大家讲道理 2017-05-16 13:37:30
0
1
695

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());
};
大家讲道理
大家讲道理

光阴似箭催人老,日月如移越少年。

reply all(1)
Peter_Zhu

f1 f2 is the return of resolve.
If you want to handle the return of reject, please use try catch

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!