条件承诺链
Conditional Promise Chaining
我得到一个 args 数组作为参数,然后我根据下面的算法进行了大量的服务器调用。
Post 以 args 数组作为数据的端点 /abc。
遍历 args 数组,
一个。一次拉取 3 个并发送 3 个 Get 调用到端点 /pqr
b。一旦步骤“2.a”中的 3 次调用成功发送 3 Post 次调用到端点 /def
c。收集步骤“2.a”服务器调用的响应并将其推送到一个数组中。
d。重复步骤 a、b、c 直到参数长度。
下面给出了整个过程的代码片段,执行从函数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);
});
};
}
我面临几个问题
- 2.c 似乎工作不正常“从步骤‘2.a’服务器调用中收集响应并将其推送到数组中。”。错误:预期 return 箭头函数中的值。我在这里做错了什么?请注意,最后我只关心步骤 2.a 的响应。
- 这是正确的链接还是可以根据上述要求进行优化?
- 我还需要做其他故障处理吗?
可能是这样 -- 我认为 batchArgs.map
中的每个项目都应该是 Promise
?然后 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);
})
你的文字是一堵砖墙,因此有点难以解读你实际想要实现的目标,但我会根据给出的代码给出我的两分钱。
//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){
});
我得到一个 args 数组作为参数,然后我根据下面的算法进行了大量的服务器调用。
Post 以 args 数组作为数据的端点 /abc。
遍历 args 数组,
一个。一次拉取 3 个并发送 3 个 Get 调用到端点 /pqr
b。一旦步骤“2.a”中的 3 次调用成功发送 3 Post 次调用到端点 /def
c。收集步骤“2.a”服务器调用的响应并将其推送到一个数组中。
d。重复步骤 a、b、c 直到参数长度。
下面给出了整个过程的代码片段,执行从函数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); }); }; }
我面临几个问题
- 2.c 似乎工作不正常“从步骤‘2.a’服务器调用中收集响应并将其推送到数组中。”。错误:预期 return 箭头函数中的值。我在这里做错了什么?请注意,最后我只关心步骤 2.a 的响应。
- 这是正确的链接还是可以根据上述要求进行优化?
- 我还需要做其他故障处理吗?
可能是这样 -- 我认为 batchArgs.map
中的每个项目都应该是 Promise
?然后 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);
})
你的文字是一堵砖墙,因此有点难以解读你实际想要实现的目标,但我会根据给出的代码给出我的两分钱。
//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){
});