Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
462 views
in Technique[技术] by (71.8m points)

javascript - 异步功能+等待+ setTimeout的组合(Combination of async function + await + setTimeout)

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.(问题是,我的while循环运行得太快,脚本每秒向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.(如果还有其他方法可以延迟请求,请告诉我。) 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.(请求的响应将返回到setTimeout中的匿名异步函数,但是我只是不知道如何将响应返回给睡眠函数resp。) to the initial asyncGenerator function.(到初始的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函数返回它,在匿名函数中回调,等等。)   ask by JShinigami translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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秒钟。)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...