标签:pad val await xxxx 输出 异步操作 xxxxx 顺序 time
const promisify = require(‘util‘).promisify
const readFile = promisify(require(‘fs‘).readFile)
const read = async function () {
let r1 = await readFile(‘驿外.txt‘, ‘utf-8‘)
console.log(1);
let r2 = await readFile(‘桃夭.txt‘, ‘utf-8‘)
console.log(2);
let r3 = await readFile(‘孔雀东南飞.txt‘, ‘utf-8‘)
console.log(3);
console.log(r1 + r2 + r3);
const timeOut = new Promise((suc, err) => {
setTimeout(() => {
suc(‘到时间了‘)
}, 2000)
})
let r4 = await timeOut
console.log(r4);
//等r4解决了才会输出
console.log(r1);
const timeOut2 = new Promise((suc, err) => {
setTimeout(() => {
//这个promise会老老实实的等上一个解决了才会开始解决
console.log(‘xxxxxx‘);
suc(‘我是第二个定时器‘)
}, 1000)
})
let r5 = await timeOut2
console.log(r5);
return ‘async函数里的await(等待的)promise全部完成!‘
}
console.log(read().then(value => { console.log(value); }));
标签:pad val await xxxx 输出 异步操作 xxxxx 顺序 time
原文地址:https://www.cnblogs.com/dingtongya/p/14781464.html