promise.all
const fs = require('fs');function readFile(filePath) {return new Promise((resolve, reject) => {fs.readFile(filePath, (err, data) => {if (err) {return reject(err)}resolve(data)})})}/** Promise.all方法接收一个数组参数,数组中是一个个的promise对象,* 该方法的返回值也是一个promise对象** */Promise.all([readFile('./a.txt'),readFile('./b.txt'),readFile('.c.txt')]).then(data=>{console.log(data[0].toString());console.log(data[1].toString());console.log(data[2].toString());}).catch((err)=>{console.log(err);});

浙公网安备 33010602011771号