[英]Combination of async function + await + setTimeout
我正在尝试使用新的异步功能,我希望解决我的问题能在未来帮助其他人。 这是我正在运行的代码:
async function asyncGenerator() {
// other code
while (goOn) {
// other code
var fileList = await listFiles(nextPageToken);
var parents = await requestParents(fileList);
// other code
}
// other code
}
function listFiles(token) {
return gapi.client.drive.files.list({
'maxResults': sizeResults,
'pageToken': token,
'q': query
});
}
问题是,我的 while 循环运行得太快,脚本每秒向 google API 发送太多请求。因此我想构建一个 sleep function 来延迟请求。 因此我也可以使用这个 function 来延迟其他请求。 如果有其他方法可以延迟请求,请告诉我。
无论如何,这是我的新代码,它不起作用。 请求的响应在 setTimeout 内返回给匿名异步 function,但我不知道如何将响应返回给睡眠 function resp。 到初始的 asyncGenerator function。
async function asyncGenerator() {
// other code
while (goOn) {
// other code
var fileList = await sleep(listFiles, nextPageToken);
var parents = await requestParents(fileList);
// other code
}
// other code
}
function listFiles(token) {
return gapi.client.drive.files.list({
'maxResults': sizeResults,
'pageToken': token,
'q': query
});
}
async function sleep(fn, par) {
return await setTimeout(async function() {
await fn(par);
}, 3000, fn, par);
}
我已经尝试了一些选项:将响应存储在全局变量中并从睡眠 function 返回它,匿名 function 中的回调等。
您的sleep
功能不起作用,因为setTimeout
还没有(还?)返回一个可以await
的承诺。 您需要手动承诺它:
function timeout(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
async function sleep(fn, ...args) {
await timeout(3000);
return fn(...args);
}
顺便说一句,要减慢您的循环速度,您可能不想使用接收回调并像这样推迟它的sleep
函数。 我建议:
while (goOn) {
// other code
var [parents] = await Promise.all([
listFiles(nextPageToken).then(requestParents),
timeout(5000)
]);
// other code
}
这让parents
的计算至少需要 5 秒。
快速单行、内联方式
await new Promise(resolve => setTimeout(resolve, 1000));
从 Node 7.6 开始,您可以将 utils 模块中的promisify
函数与setTimeout()
。
const sleep = require('util').promisify(setTimeout)
const sleep = m => new Promise(r => setTimeout(r, m))
(async () => {
console.time("Slept for")
await sleep(3000)
console.timeEnd("Slept for")
})()
setTimeout
不是async
函数,因此您不能将它与 ES7 async-await 一起使用。 但是您可以使用 ES6 Promise实现您的sleep
功能:
function sleep (fn, par) {
return new Promise((resolve) => {
// wait 3s before calling fn(par)
setTimeout(() => resolve(fn(par)), 3000)
})
}
然后你就可以在 ES7 async-await 中使用这个新的sleep
功能:
var fileList = await sleep(listFiles, nextPageToken)
请注意,我只是回答您关于将 ES7 async/await 与setTimeout
相结合的问题,尽管它可能无法解决您每秒发送过多请求的问题。
更新:现代 node.js 版本有一个内置的异步超时实现,可通过util.promisify助手访问:
const {promisify} = require('util');
const setTimeoutAsync = promisify(setTimeout);
await setTimeout
终于在 Node.js 16 中出现,不再需要使用util.promisify()
:
import { setTimeout } from 'timers/promises';
(async () => {
const result = await setTimeout(2000, 'resolved')
// Executed after 2 seconds
console.log(result); // "resolved"
})()
官方 Node.js 文档: Timers Promises API (已在 Node 中内置库)
如果您想使用与setTimeout
相同类型的语法,您可以编写这样的辅助函数:
const setAsyncTimeout = (cb, timeout = 0) => new Promise(resolve => {
setTimeout(() => {
cb();
resolve();
}, timeout);
});
然后你可以这样称呼它:
const doStuffAsync = async () => {
await setAsyncTimeout(() => {
// Do stuff
}, 1000);
await setAsyncTimeout(() => {
// Do more stuff
}, 500);
await setAsyncTimeout(() => {
// Do even more stuff
}, 2000);
};
doStuffAsync();
我做了一个要点: https : //gist.github.com/DaveBitter/f44889a2a52ad16b6a5129c39444bb57
var testAwait = function () {
var promise = new Promise((resolve, reject) => {
setTimeout(() => {
resolve('Inside test await');
}, 1000);
});
return promise;
}
var asyncFunction = async function() {
await testAwait().then((data) => {
console.log(data);
})
return 'hello asyncFunction';
}
asyncFunction().then((data) => {
console.log(data);
});
//Inside test await
//hello asyncFunction
这是我现在在 2020 年在 AWS labdas 中使用 nodejs 的版本
const sleep = require('util').promisify(setTimeout)
async function f1 (some){
...
}
async function f2 (thing){
...
}
module.exports.someFunction = async event => {
...
await f1(some)
await sleep(5000)
await f2(thing)
...
}
await new Promise(resolve => setTimeout(() => { resolve({ data: 'your return data'}) }, 1000))
await setTimeout(()=>{}, 200);
如果您的 Node 版本是 15 及更高版本,则可以使用。
以下代码适用于 Chrome 和 Firefox,也可能适用于其他浏览器。
function timeout(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
async function sleep(fn, ...args) {
await timeout(3000);
return fn(...args);
}
但是在 Internet Explorer 中,我收到"(resolve **=>** setTimeout..."
的语法错误)
基本上传入一个done
回调函数在操作完成时调用。
// Function to timeout if a request is taking too long
const setAsyncTimeout = (cb, timeout = 0) => new Promise((resolve, reject) => {
cb(resolve);
setTimeout(() => reject('Request is taking too long to response'), timeout);
});
这是我如何使用它:
try {
await setAsyncTimeout(async done => {
const requestOne = await someService.post(configs);
const requestTwo = await someService.get(configs);
const requestThree = await someService.post(configs);
done();
}, 5000); // 5 seconds max for this set of operations
}
catch (err) {
console.error('[Timeout] Unable to complete the operation.', err);
}
async function sayHello(name) {
let greet = `Hey! ${name} very nice to meet you bud.`;
setTimeout(() => {
return {
greet,
createdAt: new Date(),
};
}, 1000);
}
const response1 = async () => await sayHello("sounish");
const response2 = async () => await sayHello("alex");
const response3 = async () => await sayHello("bill");
async function getData() {
const data1 = await sayHello("sounish");
const data2 = await sayHello("alex");
const data3 = await sayHello("bill");
return { data1, data2, data3 };
}
Promise.all([sayHello("sounish"), sayHello("alex"), sayHello("bill")]).then(
(allResponses) => {
console.log({ allResponses });
}
);
getData().then((allData) => {
console.log({ allData });
});
我将这段代码片段留给想要使用setTimeout
获取 API 调用(例如获取客户端)的人:
const { data } = await new Promise(resolve => setTimeout(resolve, 250)).then(() => getClientsService())
setName(data.name || '')
setEmail(data.email || '')
我想指出一个对Promise.all
的强大扩展。 与一个 promise 一起工作的一个相当优雅的解决方案是只限时使用 promise 超时(例如new Promise((resolve) => setTimeout(resolve, timeout))
)。
await new Promise.race([myPromise, timeoutPromise])
一旦承诺之一完成,将继续。 myPromise
然后可以在内部等待不同的超时,或者简单地使用Promise.all
const timeout = ms => new Promise((resolve) => setTimeout(resolve, ms));
await Promise.race([
Promise.all([myPromise, timeout(500)]),
timeout(5000)
]);
结果是一个异步调用,每秒运行的频率不超过两次,如果出现某些(网络/服务器?)错误,超时时间为 5 秒。
此外,您可以使这个非常通用和可定制的 function 像这样:
function callWithTimeout(promise, msTimeout=5000, throws=false) {
const timeout = ms => new Promise((resolve, reject) =>
setTimeout(throws ? reject : resolve, ms));
await Promise.race([
//depends whether you want to wait there or just pass the promise itself
Promise.all([promise, timeout(500)]),
timeout(msTimeout)
]);
}
最终,您可以自定义超时时间以及 promise 应该成功还是超时。 拥有如此强大的通用实现可以在未来为您节省很多痛苦。 您还可以设置一个字符串而不是 boolean 作为throws
并将此变量绑定到reject
自定义错误消息: reject.bind(undefined, throws)
请注意,您不应将 promise 与 await 一起传递:
const myPromise = async x => x;
//will never time out and not because myPromise will finish immediatelly
callWithTimeout(await myPromise(), 200, true);
//will possibly timeout after 200 ms with an exception
callWithTimeout(myPromise(), 200, true);
这是单线中的一种更快的解决方法。
希望这会有所帮助。
// WAIT FOR 200 MILISECONDS TO GET DATA //
await setTimeout(()=>{}, 200);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.