由于该事件仅用作继续处理的信号,因此您可以在继续使用内嵌样式代码之前承诺并await
确认其接收。这也意味着result
不需要在嵌套函数的外部范围中声明(由于异步设置,该函数始终无法工作):
async function checkSomething() {
const response = await axios.get('https://someUrl.com',{
responseType: 'stream'
});
const file = fs.createWriteStream('./localFile.txt');
response.data.pipe(file);
await new Promise( resolve=> file.on(`finish`,resolve)); // wait here
const result = await performATask();
return result == 'good`;
}
,
您已经很接近了,您只需要返回一个承诺,该承诺将在执行达到您的期望点(您有评论的地方)后解决。
其他注意事项:
- 确保仅在
await
个函数中使用async
- 使用
===
代替==
- 以
;
s结尾的语句
const axios = require('axios');
const fs = require('fs');
/*....*/
let isItChecked = await checkSomething();
async function checkSomething() {
const response = await axios.get('https://someUrl.com',{responseType: 'stream'});
const file = fs.createWriteStream('./localFile.txt');
response.data.pipe(file);
return new Promise(resolve =>
file.on('finish',() =>
performATask().then(result => resolve(result === 'good'))));
}
本文链接:https://www.f2er.com/3148559.html