weixin_33720078 2017-09-17 04:10 采纳率: 0%
浏览 20

有条件的承诺链

I get an array of args as an argument, and then I make lots of server calls based on the algo below.

  1. Post to endpoint /abc with args array as data.
  2. Iterate over args array,

    a. Pull 3 at a time and send 3 Get calls to endpoint /pqr

    b. Once 3 calls in step '2.a' succeeds send 3 Post calls to endpoint /def

    c. Collect responses from step '2.a' server call and push it in an array.

    d. Repeat step a,b,c till args length.

Code Snippet for the entire process is given below, execution starts at function execute(args).

import Promise from 'bluebird';

import request from 'superagent';

// sends a post request to server 
const servercall2 = (args, response) => {

        const req = request
            .post(`${baseUrl}/def`)
            .send(args, response)
            .setAuthHeaders();

        return req.endAsync();
};

// sends a post request to server
const servercall1 = (args) => {

        const req = request
            .post(`${baseUrl}/abc`)
            .send(args)
            .setAuthHeaders();

        return req.endAsync()
            .then((res) => resolve({res}))
            .catch((err) => reject(err));
};

async function makeServerCalls(args, length) {

    // convert args to two dimensional array, chunks of given length [[1,2,3], [4,5,6,], [7,8]]

    const batchedArgs = args.reduce((rows, key, index) => (index % length === 0 ? rows.push([key])
        : rows[rows.length - 1].push(key)) && rows, []);

    const responses = [];

    for (const batchArgs of batchedArgs) {
        responses.push(
            // wait for a chunk to complete, before firing the next chunk of calls
            await Promise.all(

                ***// Error, expected to return a value in arrow function???***
                batchArgs.map((args) => {
                    const req = request
                        .get(`${baseUrl}/pqr`)
                        .query(args)

                    // I want to collect response from above req at the end of all calls.
                    return req.endAsync()
                        .then((response) =>servercall2(args,response)); 
                })
            )
        );
    }

    // wait for all calls to finish
    return Promise.all(responses);
}

export function execute(args) {
    return (dispatch) => {

       servercall1(args)
           .then(makeServerCalls(args, 3))
           .then((responses) => {
                    const serverresponses = [].concat(...responses);
                    console.log(serverresponses);
            });
    };
}

I am facing couple of issues

  1. 2.c seems not to be working fine "Collect responses from step '2.a' server call and push it in an array.". Error: expected to return a value in arrow function. What am I doing wrong here? Please note that at the end I care about the response from step 2.a only.
  2. Is this a right chaining or it can be optimized, based on the requirements mentioned above?
  3. Is there any other failure handling I have to do?
  • 写回答

2条回答 默认 最新

  • Memor.の 2017-09-17 04:22
    关注

    It might be this -- each item in batchArgs.map should be a Promise I think? Then Promise.all will wait for each to finish:

    batchArgs.map((args) => {
        const req = request
            .get(`${baseUrl}/pqr`)
            .query(args)
    
    
        // Return promise here
        return req.endAsync()
            .then((response) =>servercall2(args,response))
            .then((res) => res);
    })
    
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog