search

Home  >  Q&A  >  body text

javascript - Where to write the callback method of the Promise object behind await

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

reply all(1)I'll reply

  • 天蓬老师

    天蓬老师2017-05-16 13:39:30

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

    reply
    0
  • Cancelreply