rateltalk
V2EX  ›  Node.js

Nodejs 如何并发请求数据接口,并返回汇总后的结果?

  •  
  •   rateltalk · Aug 20, 2020 · 3795 views
    This topic created in 2127 days ago, the information mentioned may be changed or developed.

    我用 Nodejs 写了一个 API 接口,用于请求指定数据。这个接口会请求三个其他的 API:

    1. http://a.com/api1
    2. http://b.com/api2
    3. http://c.com/api3

    我的目的是同时(并发)请求上面这三个 API,然后汇总请求到的结果,返回给客户端。
    请问这个可以实现吗,对 Nodejs 不懂?

    11 replies    2020-09-21 11:52:45 +08:00
    hgjian
        1
    hgjian  
       Aug 20, 2020 via Android
    promise.all ?好像可以
    flowfire
        2
    flowfire  
       Aug 20, 2020   ❤️ 1
    await Promise.all([
    fetch("url1"),
    fetch("url2"),
    fetch("url3"),
    ])
    c6h6benzene
        3
    c6h6benzene  
       Aug 20, 2020 via iPhone
    如果用包装好的包如 axios,可以用 axios.all([request1, request2, request3,...).then(axios.spread(response1, response2, response3) => {})。
    coderxy
        4
    coderxy  
       Aug 20, 2020
    promise.all
    4196
        5
    4196  
       Aug 20, 2020
    不是 Nodejs 不懂的问题,你应该多去学下 js,特别是 es6+的😄
    dfourc
        6
    dfourc  
       Aug 20, 2020
    1.回调嵌套
    2.请求成功计数
    3.promise.all
    buffzty
        7
    buffzty  
       Aug 20, 2020
    async ()=>{
    const task1 = fetch1()
    const task2 = fetch2()
    const task3 = fetch3()
    // try catch
    const task1Resp = await task1
    const task2Resp = await task2
    const task3Resp = await task3
    // logic
    }
    azcvcza
        8
    azcvcza  
       Aug 20, 2020   ❤️ 1
    promise.all 建议里边包一层 promise,这样即使请求错误 promise.all 也不会挂掉往下走了
    evilStart
        9
    evilStart  
       Aug 20, 2020 via Android
    @azcvcza promise.allSettled 可解
    azcvcza
        10
    azcvcza  
       Aug 21, 2020
    @evilStart 估计也是 promise.all 里包一层 promise 的语法糖
    JaydenC
        11
    JaydenC  
       Sep 21, 2020
    用这个就好了,总有你想要的流程控制方式
    https://caolan.github.io/async/v3/docs.html#parallel



    async.parallel([
    function(callback) {
    setTimeout(function() {
    callback(null, 'one');
    }, 200);
    },
    function(callback) {
    setTimeout(function() {
    callback(null, 'two');
    }, 100);
    }
    ],
    // optional callback
    function(err, results) {
    // the results array will equal ['one','two'] even though
    // the second function had a shorter timeout.
    });

    // an example using an object instead of an array
    async.parallel({
    one: function(callback) {
    setTimeout(function() {
    callback(null, 1);
    }, 200);
    },
    two: function(callback) {
    setTimeout(function() {
    callback(null, 2);
    }, 100);
    }
    }, function(err, results) {
    // results is now equals to: {one: 1, two: 2} 这里执行所有请求返回后的回调
    });
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3083 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 68ms · UTC 13:22 · PVG 21:22 · LAX 06:22 · JFK 09:22
    ♥ Do have faith in what you're doing.