【问题标题】:How to guarantee all requests are fulfilled asynchronously, and map the request url to the promise response in Node.js 10.x.x?在 Node.js 10.x.x 中如何保证所有请求都异步完成,并将请求 url 映射到 promise 响应?
【发布时间】:2021-07-04 18:37:08
【问题描述】:

我有几个 GET 请求需要发出,由于超时限制,它们必须异步运行,我需要保证全部运行。另外,当结果返回时,我需要知道它被引用到哪个 URL。

我设法解决了第一部分,但我无法映射到正确的 url。代码如下:

urls = ['https://example-a.com', 'https://example-b.com', 'https://example-c.com']

Promise.all(promises.map(p => fetch(p.url))).then(function(res){
    var blobPromises = [];

    /* HERE I WOULD LIKE TO KNOW WHICH URL'S REQUEST WAS RETURNED */
    
    for (var j = 0; j < urls.length - 1; j++) {
        blobPromises.push(res[j].text());
    }
    return Promise.all(blobPromises);
}).then(function(body){
    var output = {rawData: body)};
    callback(null, output);
}).catch(callback);

如何进行这种映射?

【问题讨论】:

    标签: javascript node.js promise fetch


    【解决方案1】:

    您似乎错误地假设 Promise.all 的 then 处理程序为每个承诺调用一次。相反,它会调用一次(或者如果不是所有的 Promise 都解析),则使用包含每个 Promise 的解析的数组(与它们的 Promise 的顺序相同)。

    let promises = [Promise.resolve(1), Promise.resolve(2), Promise.resolve(3)];
    Promises.all(promises).then(resolveObjects => 
      console.log(resolveObjects)); // [1, 2, 3]
    

    对于您的示例(稍作修改,因为您的代码缺少一些变量和不正确的语句):

    urls = ['https://example-a.com', 'https://example-b.com', 'https://example-c.com']
    
    let promises = urls.map(url => fetch(url));
    
    let responses = Promise.all(promises).then(responses => 
        responses.map(respnose => response.text()));
    

    (旁注),异步流依赖项(例如https://www.npmjs.com/package/bs-better-stream)将允许您将承诺数组或承诺数组(或承诺数组的承诺......)更像普通数组。

    let responses = new Stream()
      .writePromise(...promises)
      .map(response => response.text());
    

    【讨论】:

    • 非常感谢!我不明白带有已解决承诺的数组将保证与承诺的顺序相同。
    猜你喜欢
    • 1970-01-01
    • 2015-07-17
    • 2011-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-17
    相关资源
    最近更新 更多