Your sleep
function does not work because setTimeout
does not (yet?) return a promise that could be await
ed.(您的sleep
功能不起作用,因为setTimeout
不会(尚未?)返回可能await
的承诺。)
You will need to promisify it manually:(您将需要手动启用它:)
function timeout(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
async function sleep(fn, ...args) {
await timeout(3000);
return fn(...args);
}
Btw, to slow down your loop you probably don't want to use a sleep
function that takes a callback and defers it like this.(顺便说一句,要减慢循环速度,您可能不想使用带有回调并以此延迟的sleep
功能。) I'd rather recommend to do something like(我宁愿建议做类似的事情)
while (goOn) {
// other code
var [parents] = await Promise.all([
listFiles(nextPageToken).then(requestParents),
timeout(5000)
]);
// other code
}
which lets the computation of parents
take at least 5 seconds.(这使得parents
计算至少需要5秒钟。) 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…