const fs = require('fs');

const readFile = function (fileName) {
  return new Promise(function (resolve, reject) {
    fs.readFile(fileName, "utf-8",function(error, data) {
      if (error) return reject(error);
      resolve(data);
    });
  });
};

const gen = async function() {
	
  const f1 = await readFile('./data1.txt');
  const f2 = await readFile('./data2.txt');
  console.log(f1);
  console.log(f2);
};

gen();

  上面是读取两个文件的例子

和co模块相比较

co模块约定,yield命令后面只能是 Thunk 函数或 Promise 对象,而async函数的await命令后面,可以是 Promise 对象和原始类型的值(数值、字符串和布尔值,但这时等同于同步操作)。

相关文章:

  • 2021-05-08
  • 2021-08-08
  • 2022-12-23
  • 2021-10-31
  • 2021-10-12
  • 2021-10-12
  • 2022-12-23
  • 2021-09-10
猜你喜欢
  • 2021-10-24
  • 2021-10-01
  • 2021-05-22
  • 2021-11-25
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案