孤陋寡闻,这是什么神仙写法?
for循环有个await? 代码如下:
const fs = require(‘fs’);
async function print(path) { const dir = await fs.promises.opendir(path);
for await (const dirent of dir) {
console.log(dirent.name);
}
} print(’./’).catch(console.error);
3 回复
https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Statements/for-await…of 流行不起来,不好用,我反正老早就知道这个了,但从来没用过
let timeout = async (timeout) => new Promise((resolve, reject) => {
setTimeout(() => {
console.log(`delay:${timeout}ms`)
resolve();
}, timeout);
});
async function* getArray(){
for (let i=0;i<5000;i+=1000){
await timeout(i);
yield i;
}
return;
}
(async()=>{
for await (const time of getArray()) {
console.log(`time:${time}`);
}
// for (const time of getArray()) {
// console.log(`time:${time}`);
// }
})();
既然下面使用了 for-await-of ,那么上面的dir赋值是否就不应该用 await 了呢?