相关疑难解决方法(0)

async函数+ 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
    });
  }
Run Code Online (Sandbox Code Playgroud)

问题是,我的while循环运行得太快,并且脚本每秒向google API发送太多请求.因此,我想建立一个延迟请求的睡眠功能.因此我也可以使用此函数来延迟其他请求.如果有其他方式延迟请求,请告诉我.

无论如何,这是我的新代码不起作用.请求的响应返回到setTimeout中的匿名异步函数,但我只是不知道如何将响应返回给sleep函数resp.到最初的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
  } …
Run Code Online (Sandbox Code Playgroud)

javascript settimeout async-await ecmascript-2017

219
推荐指数
10
解决办法
17万
查看次数