[英]Conditional Promise Chaining
I get an array of args as an argument, and then I make lots of server calls based on the algo below. 我得到一个args数组作为参数,然后根据下面的算法进行许多服务器调用。
Iterate over args array, 遍历args数组,
a. 一种。 Pull 3 at a time and send 3 Get calls to endpoint /pqr
一次拉3个并向端点/ pqr发送3个Get呼叫
b. b。 Once 3 calls in step '2.a' succeeds send 3 Post calls to endpoint /def
一旦在步骤'2.a'中成功进行3次呼叫,就向端点/ def发送3个Post呼叫
c. C。 Collect responses from step '2.a' server call and push it in an array.
从步骤“ 2.a”服务器调用中收集响应,并将其推送到数组中。
d. d。 Repeat step a,b,c till args length.
重复步骤a,b,c,直到args长度。
Code Snippet for the entire process is given below, execution starts at function execute(args). 整个过程的代码段如下所示,执行从函数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 我面临几个问题
It might be this -- each item in batchArgs.map
should be a Promise
I think? 可能就是这样
batchArgs.map
每个项目batchArgs.map
应该是一个我认为的Promise
? Then Promise.all
will wait for each to finish: 然后
Promise.all
将等待每个完成:
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);
})
You have a brick wall of text so its becoming a little hard to decipher what you're actually trying to achieve, but I will give my two cents on the code given. 您的文本是一堵砖墙,因此很难理解您实际上要实现的目标,但是我将在给出的代码中投入2美分。
//Both server calls can be simplified.. no need to
//wrap in another promise if one is being returned
const servercall2 = (args, response) => {
const req = request
.post(`${baseUrl}/def`)
.send(args, response)
.setAuthHeaders();
return req.endAsync();
};
//Here... you return no value in the function passed to map, thus an
//error is being thrown. You need to return a Promise from here so that
//it can be passed into Promise.all
const allFinished = await Promise.all(
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()
})
);
allFinished.then(function(results){
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.