Javascript async函数+await+setTimeout的组合
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33289726/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Combination of async function + await + setTimeout
提问by JShinigami
I am trying to use the new async features and I hope solving my problem will help others in the future. This is my code which is working:
我正在尝试使用新的异步功能,我希望解决我的问题将在未来帮助其他人。这是我正在运行的代码:
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
});
}
The problem is, that my while loop runs too fast and the script sends too many requests per second to the google API. Therefore I would like to build a sleep function which delays the request. Thus I could also use this function to delay other requests. If there is another way to delay the request, please let me know.
问题是,我的 while 循环运行速度太快,脚本每秒向 google API 发送太多请求。因此,我想构建一个延迟请求的睡眠功能。因此我也可以使用这个函数来延迟其他请求。如果有其他方法可以延迟请求,请告诉我。
Anyway, this is my new code which does not work. The response of the request is returned to the anonymous async function within the setTimeout, but I just do not know how I can return the response to the sleep function resp. to the initial asyncGenerator function.
无论如何,这是我的新代码,它不起作用。请求的响应在 setTimeout 内返回给匿名异步函数,但我不知道如何将响应返回给睡眠函数。到初始 asyncGenerator 函数。
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);
}
I have already tried some options: storing the response in a global variable and return it from the sleep function, callback within the anonymous function, etc.
我已经尝试了一些选项:将响应存储在全局变量中并从 sleep 函数返回它,匿名函数中的回调等。
回答by Bergi
Your sleep
function does not work because setTimeout
does not (yet?) return a promise that could be await
ed. You will need to promisify it manually:
您的sleep
函数不起作用,因为setTimeout
还没有(还?)返回一个可以被await
编辑的承诺。您需要手动承诺它:
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. I'd rather recommend to do something like
顺便说一句,要减慢循环的速度,您可能不想使用sleep
接受回调并像这样延迟的函数。我宁愿建议做类似的事情
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 秒。
回答by Harry
Since Node 7.6, you can combine the functions promisify
function from the utils module with setTimeout()
.
从 Node 7.6 开始,您可以将promisify
utils 模块中的函数功能与setTimeout()
.
Node.js
节点.js
const sleep = require('util').promisify(setTimeout)
Javascript
Javascript
const sleep = m => new Promise(r => setTimeout(r, m))
Usage
用法
(async () => {
console.time("Slept for")
await sleep(3000)
console.timeEnd("Slept for")
})()
回答by FlavorScape
The quick one-liner, inline way
快速单行、内联方式
await new Promise(resolve => setTimeout(resolve, 1000));
回答by Leonid Beschastny
setTimeout
is not an async
function, so you can't use it with ES7 async-await. But you could implement your sleep
function using ES6 Promise:
setTimeout
不是一个async
函数,所以你不能在 ES7 async-await 中使用它。但是您可以sleep
使用 ES6 Promise实现您的功能:
function sleep (fn, par) {
return new Promise((resolve) => {
// wait 3s before calling fn(par)
setTimeout(() => resolve(fn(par)), 3000)
})
}
Then you'll be able to use this new sleep
function with ES7 async-await:
然后你就可以在sleep
ES7 的 async-await 中使用这个新函数:
var fileList = await sleep(listFiles, nextPageToken)
Please, notethat I'm only answering your question about combining ES7 async/await with setTimeout
, though it may not help solve your problem with sending too many requests per second.
请注意,我只是回答您关于将 ES7 async/await 与 结合的问题setTimeout
,尽管它可能无法解决您每秒发送过多请求的问题。
Update:Modern node.js versions has a buid-in async timeout implementation, accessible via util.promisifyhelper:
更新:现代 node.js 版本有一个内置的异步超时实现,可通过util.promisify助手访问:
const {promisify} = require('util');
const setTimeoutAsync = promisify(setTimeout);
回答by Dave Bitter
If you would like to use the same kind of syntax as setTimeout
you can write a helper function like this:
如果您想使用与setTimeout
您可以编写这样的辅助函数相同的语法:
const setAsyncTimeout = (cb, timeout = 0) => new Promise(resolve => {
setTimeout(() => {
cb();
resolve();
}, timeout);
});
You can then call it like so:
然后你可以这样称呼它:
const doStuffAsync = async () => {
await setAsyncTimeout(() => {
// Do stuff
}, 1000);
await setAsyncTimeout(() => {
// Do more stuff
}, 500);
await setAsyncTimeout(() => {
// Do even more stuff
}, 2000);
};
doStuffAsync();
I made a gist: https://gist.github.com/DaveBitter/f44889a2a52ad16b6a5129c39444bb57
我做了一个要点:https: //gist.github.com/DaveBitter/f44889a2a52ad16b6a5129c39444bb57
回答by vignesh
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
回答by Jee Mok
Made a util inspired from Dave's answer
Basically passed in a done
callback to call when the operation is finished.
基本上传入一个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);
});
This is how I use it:
这是我如何使用它:
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);
}
回答by zwitterion
This is my version with nodejs now in 2020 in AWS labdas
这是我现在在 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)
...
}
回答by Shadowned
The following code works in Chrome and Firefox and maybe other browsers.
以下代码适用于 Chrome 和 Firefox,也可能适用于其他浏览器。
function timeout(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
async function sleep(fn, ...args) {
await timeout(3000);
return fn(...args);
}
But in Internet Explorer I get a Syntax Error for the "(resolve **=>** setTimeout..."
但是在 Internet Explorer 中,我收到了一个语法错误 "(resolve **=>** setTimeout..."
回答by Rommy Garg
This is a quicker fix in one-liner.
这是单线中的一种更快的解决方法。
Hope this will help.
希望这会有所帮助。
// WAIT FOR 200 MILISECONDS TO GET DATA //
await setTimeout(()=>{}, 200);