为什么不等待等待的 Promise.all 解决?

时间:2021-05-11 22:22:54

标签: javascript async-await promise axios

我以前的工作代码在迭代的每个元素上都低效地调用了等待。我正在重构以使用 Promise.All。 但是,在执行进一步代码之前,我的代码不会等待 Promise.All 解决。

具体来说,purgeRequestPromises 行在初始 Promise.All 解析之前执行。我不确定这是为什么? retrieveSurrogateKey 是一个异步函数,因此它的返回行将包含在一个已解决的 promise 中。

try {
    //retrieve surrogate key associated with each URL/file updated in push to S3
    const surrogateKeyPromises = urlArray.map(url => this.retrieveSurrogateKey(url));
    const surrogateKeyArray = await Promise.all(surrogateKeyPromises).catch(console.log);

    //purge each surrogate key
     const purgeRequestPromises = surrogateKeyArray.map(surrogateKey => this.requestPurgeOfSurrogateKey(surrogateKey));
     await Promise.all(purgeRequestPromises);

     // GET request the URLs to warm cache for our users
     const warmCachePromises = urlArray.map(url => this.warmCache(url));
     await Promise.all(warmCachePromises)
} catch (error) {
    logger.save(`${'(prod)'.padEnd(15)}error in purge cache: ${error}`);
    throw error
} 

async retrieveSurrogateKey(url) {
    try {
        axios({
            method: 'HEAD',
            url: url,
            headers: headers,
        }).then(response => {
            console.log("this is the response status: ", response.status)
            if (response.status === 200) {
                console.log("this is the surrogate key!! ", response.headers['surrogate-key'])
                return response.headers['surrogate-key'];
            }

        });
    } catch (error) {
        logger.save(`${'(prod)'.padEnd(15)}error in retrieveSurrogateKey: ${error}`);
        throw error
    }
}

我知道 purgeRequestPromises 会提前执行,因为我收到错误,抱怨我在 HEAD 请求中将 Surrogate-Key 标头设置为 undefined

async requestPurgeOfSurrogateKey(surrogateKey) {
    headers['Surrogate-Key'] = surrogateKey

    try {
        axios({
                method: `POST`,
                url: `https://api.fastly.com/service/${fastlyServiceId}/purge/${surrogateKey}`,
                path: `/service/${fastlyServiceId}/purge${surrogateKey}`,
                headers: headers,
            })
            .then(response => {
                console.log("the status code for purging!! ", response.status)
                if (response.status === 200) {
                    return true
                }
            });
    } catch (error) {
        logger.save(`${'(prod)'.padEnd(15)}error in requestPurgeOfSurrogateKey: ${error}`);
        throw error;
    }
}

2 个答案:

答案 0 :(得分:3)

retrieveSurrogateKey 同步返回 undefinedtry 块中的值是一个 promise 并且不会同步抛出错误,因此永远不会执行 catch 子句并执行掉出底部,从函数体返回 undefined

您可以尝试以下操作:

function retrieveSurrogateKey(url) {  // returns a promise
    return axios({
//  ^^^^^^
        method: 'HEAD',
        url: url,
        headers: headers,
    }).then(response => {
        console.log("this is the response status: ", response.status)
        if (response.status === 200) {
            console.log("this is the surrogate key!! ", response.headers['surrogate-key'])
            return response.headers['surrogate-key'];
        }

    }).catch(error => {
       logger.save(`${'(prod)'.padEnd(15)}error in retrieveSurrogateKey: ${error}`);
       throw error;
    });
}

请注意,如果函数不使用 async,则将返回承诺的函数声明为 await 是多余的。这一行还有一个次要问题:

const surrogateKeyArray = await Promise.all(surrogateKeyPromises).catch(console.log);

除非重新抛出错误,否则 catch 子句将履行承诺链。您可以(也许)省略 .catch 子句或将其重新编码为

.catch( err=> { console.log(err); throw err} );

答案 1 :(得分:0)

您不必从 async 中删除 retrieveSurrogateKey() 才能使其工作。事实上,如果你不这样做,它更具可读性。正如已经解释过的,问题在于 retrieveSurrogateKey() 返回的承诺没有跟随对 axios() 的调用返回的承诺的完成。你需要await

async retrieveSurrogateKey(url) {
  try {
    const response = await axios({
      method: 'HEAD',
      url,
      headers,
    });

    console.log('this is the response status: ', response.status);

    if (response.status === 200) {
      const surrogateKey = response.headers['surrogate-key'];
      console.log('this is the surrogate key!! ', surrogateKey);
      return surrogateKey;
    }
  } catch (error) {
    logger.save(`${'(prod)'.padEnd(15)}error in retrieveSurrogateKey: ${error}`);
    throw error;
  }
}

这保留了您当前拥有的相同逻辑,但您会注意到,当 response.status !== 200 时,您最终会得到 undefined 的已解决承诺,而不是被拒绝的承诺。您可能希望使用 validateStatus 来断言 200 的确切状态。默认情况下,axios 会解析状态 >= 200 和 < 300 的任何响应:

async retrieveSurrogateKey(url) {
  try {
    const response = await axios({
      method: 'HEAD',
      url,
      headers,
      validateStatus(status) {
        return status === 200;
      } 
    });
    
    const surrogateKey = response.headers['surrogate-key'];
    console.log('this is the surrogate key!! ', surrogateKey);
    return surrogateKey;
  } catch (error) {
    logger.save(`${'(prod)'.padEnd(15)}error in retrieveSurrogateKey: ${error}`);
    throw error;
  }
}

这样,您总能得到一个代理键或被拒绝的承诺。