node.js:构造多个 API 请求,处理它们并将它们组合起来
node.js: structure multiple API requests, work on them and combine them
目前我正在努力处理 node.js(我是新手)做不同的 API 请求(Usabilla API),处理结果然后将它们合并为了在整个集合上工作(例如导出)。
请求 API 不是问题,但我无法得到结果来做其他事情(异步代码让我发疯)。
请在附件中找到我想如何做到这一点的概述。也许我对此完全错了,或者你有其他更优雅的建议。
我的代码一直有效,直到我必须请求两个不同的 API "adresses"(已提供),然后提取结果以执行其他操作。
我这里的问题是嵌套函数带有承诺,我无法弄清楚如何通过瀑布内的父函数传递它以由下一个函数处理。
在代码中,当然没有如图所示的并行。
那是另一点,如何做到这一点?简单地嵌套并联和串联/瀑布内的另一个瀑布?
我有点困惑,因为当使用同步代码完成时,对于一个简单的问题会变得越来越复杂。
在这里我建立了我所有的请求查询(目前是 4):
function buildQuery(IDs,callback){
var i = 0;
var max = Object.keys(IDs).length;
async.whilst(
function(){return i < max},
function(callback){
FeedbackQuery[i] =
{
identifier: IDs[i].identifier,
query:
{id: IDs[i].id,
params: {since:sinceDate,}
}
};
i++;
callback(null,i);
})
console.log(FeedbackQuery);
callback (null,FeedbackQuery);
};
然后我必须决定它是哪种类型的查询并将其添加到一个对象中,该对象应包含此标识符类型的所有项目:
function FeedbackRequest(FeedbackQuery,callback)
{
var i = 0;
var max = Object.keys(FeedbackQuery).length;
async.whilst(
function(){return i < max},
function (callback){
identifier = FeedbackQuery[i].identifier;
APIquery = FeedbackQuery[i].query;
switch(identifier)
{
case 'mobilePortal':
console.log(FeedbackQuery[i].identifier, 'aktiviert!');
var result = api.websites.buttons.feedback.get(APIquery);
result.then(function(feedback)
{
var item = Object.keys(feedbackResults).length;
feedbackResultsA[item] = feedback;
callback(null, feedbackResultsA);
})
break;
case 'apps':
console.log(FeedbackQuery[i].identifier, 'aktiviert!');
var result = api.apps.forms.feedback.get(APIquery);
result.then(function(feedback)
{
var item = Object.keys(feedbackResults).length;
feedbackResultsB[item] = feedback;
callback(null, feedbackResultsB);
})
break;
}
i++;
callback(null,i);
})
};
目前这些函数捆绑在一个异步瀑布中:
async.waterfall([
async.apply(buildQuery,IDs2request),
FeedbackRequest,
// a function to do something on the whole feedbackResults array
],function (err, result) {
// result now equals 'done'
if (err) { console.log('Something is wrong!'); }
return console.log('Done!');
})
实际情况应该如何:
Structure
非常感谢您的任何提示或提示!
我不精通异步,我相信如果你是新手,它比一个简单的 Promise 库更难,比如 bluebird combine with lodash 的助手。
根据你的模式我会做什么:
var firstStepRequests = [];
firstStepRequests.push(buildQuery());// construct your first steps queries, can be a loop, goal is to have firstStepRequests to be an array of promise.
Promise.all(firstStepRequests)
.then((allResults) => {
var type1 = _.filter(allResults, 'request_type_1');
var type2 = _.filter(allResults, 'request_type_2');
return {
type1: type1,
type2: type2
};
})
.then((result) => {
result.type1 = //do some work
result.type2 = //do some work
return result;
})
.then((result) => {
//export or merge or whatever.
});
目标是拥有一个简单的状态机。
更新
如果你想保留请求的标识符,你可以使用道具来拥有:
var props = {
id_1:Promise,
id_2:Promise,
id_3:Promise
};
Promise.props(props).then((results) => {
// results is {
id_1:result of promise,
id_2:result of promise,
etc...
}
})
你可以这样做:
var type1Promises = getType1Requests(); //array of type 1
var type2Promises = getType2Requests(); // array of type 2
var props = {
type_1: Promise.all(type1Promises),
type_2: Promise.all(type2Promises)
}
Promise.props(props).then((result) => {
//result is : {
type_1: array of result of promises of type 1
type_2: array of result of promises of type 2
}
})
目前我正在努力处理 node.js(我是新手)做不同的 API 请求(Usabilla API),处理结果然后将它们合并为了在整个集合上工作(例如导出)。 请求 API 不是问题,但我无法得到结果来做其他事情(异步代码让我发疯)。
请在附件中找到我想如何做到这一点的概述。也许我对此完全错了,或者你有其他更优雅的建议。
我的代码一直有效,直到我必须请求两个不同的 API "adresses"(已提供),然后提取结果以执行其他操作。 我这里的问题是嵌套函数带有承诺,我无法弄清楚如何通过瀑布内的父函数传递它以由下一个函数处理。
在代码中,当然没有如图所示的并行。 那是另一点,如何做到这一点?简单地嵌套并联和串联/瀑布内的另一个瀑布? 我有点困惑,因为当使用同步代码完成时,对于一个简单的问题会变得越来越复杂。
在这里我建立了我所有的请求查询(目前是 4):
function buildQuery(IDs,callback){
var i = 0;
var max = Object.keys(IDs).length;
async.whilst(
function(){return i < max},
function(callback){
FeedbackQuery[i] =
{
identifier: IDs[i].identifier,
query:
{id: IDs[i].id,
params: {since:sinceDate,}
}
};
i++;
callback(null,i);
})
console.log(FeedbackQuery);
callback (null,FeedbackQuery);
};
然后我必须决定它是哪种类型的查询并将其添加到一个对象中,该对象应包含此标识符类型的所有项目:
function FeedbackRequest(FeedbackQuery,callback)
{
var i = 0;
var max = Object.keys(FeedbackQuery).length;
async.whilst(
function(){return i < max},
function (callback){
identifier = FeedbackQuery[i].identifier;
APIquery = FeedbackQuery[i].query;
switch(identifier)
{
case 'mobilePortal':
console.log(FeedbackQuery[i].identifier, 'aktiviert!');
var result = api.websites.buttons.feedback.get(APIquery);
result.then(function(feedback)
{
var item = Object.keys(feedbackResults).length;
feedbackResultsA[item] = feedback;
callback(null, feedbackResultsA);
})
break;
case 'apps':
console.log(FeedbackQuery[i].identifier, 'aktiviert!');
var result = api.apps.forms.feedback.get(APIquery);
result.then(function(feedback)
{
var item = Object.keys(feedbackResults).length;
feedbackResultsB[item] = feedback;
callback(null, feedbackResultsB);
})
break;
}
i++;
callback(null,i);
})
};
目前这些函数捆绑在一个异步瀑布中:
async.waterfall([
async.apply(buildQuery,IDs2request),
FeedbackRequest,
// a function to do something on the whole feedbackResults array
],function (err, result) {
// result now equals 'done'
if (err) { console.log('Something is wrong!'); }
return console.log('Done!');
})
实际情况应该如何: Structure
非常感谢您的任何提示或提示!
我不精通异步,我相信如果你是新手,它比一个简单的 Promise 库更难,比如 bluebird combine with lodash 的助手。
根据你的模式我会做什么:
var firstStepRequests = [];
firstStepRequests.push(buildQuery());// construct your first steps queries, can be a loop, goal is to have firstStepRequests to be an array of promise.
Promise.all(firstStepRequests)
.then((allResults) => {
var type1 = _.filter(allResults, 'request_type_1');
var type2 = _.filter(allResults, 'request_type_2');
return {
type1: type1,
type2: type2
};
})
.then((result) => {
result.type1 = //do some work
result.type2 = //do some work
return result;
})
.then((result) => {
//export or merge or whatever.
});
目标是拥有一个简单的状态机。
更新
如果你想保留请求的标识符,你可以使用道具来拥有:
var props = {
id_1:Promise,
id_2:Promise,
id_3:Promise
};
Promise.props(props).then((results) => {
// results is {
id_1:result of promise,
id_2:result of promise,
etc...
}
})
你可以这样做:
var type1Promises = getType1Requests(); //array of type 1
var type2Promises = getType2Requests(); // array of type 2
var props = {
type_1: Promise.all(type1Promises),
type_2: Promise.all(type2Promises)
}
Promise.props(props).then((result) => {
//result is : {
type_1: array of result of promises of type 1
type_2: array of result of promises of type 2
}
})