我有数据传递给函数,我需要检查数组中的每个对象并返回所有的promise,如何将相同的数据传递给链接,以便我可以用于处理。每个承诺都是调用数据库来验证和插入数据。
1-如何将event
传递给所有承诺以进行进一步处理?
2-如何检查是否所有事件处理都已完成,然后下一个func可以调用?
Ctrl.js
var data = [{
activityId: '1tgktxy',
variables: {
event: [Object]
},
priority: 0
},
{
activityId: '7tl07',
variables: {
caseIdFound: [{
type: Boolean,
value: true
}],
event: [Object]
},
priority: 0
},
{
activityId: '7tl07',
variables: {
caseIdFound: [{
type: Boolean,
value: true
}],
event: [Object]
},
priority: 0
},
]
function mainFunction(data) {
return Promise.all(data.map(function(element) {
var event = JSON.parse(element.variables.event.value);
return checkTicketNumber(event);
})
.then(function() {
return insertDataToLog();
}).
.then(function() {
return insertDataToResult();
})
.then(function() {
console.log('All task events processing is done');
// anotherFunction();
}).
}
var checkTicketNumber = new Promise(
function(resolve, reject) {
checkTicketCall(ticketNumber, function(err, response) {
resolve();
}):
});
// if caseIdFound is true then i want invoke thise promise
var insertDataToLog = new Promise(
function(resolve, reject) {
InsertCall(event.body, function(err, response) {
resolve();
}):
});
// if caseIdFound is true then i want invoke thise promise
var insertDatatoResult = new Promise(
function(resolve, reject) {
InsertCall(event.body, function(err, response) {
resolve();
}):
});
mainFunction(data);
答案 0 :(得分:0)
您可以使用参数来定义解析函数。
实施例
var insertDataToLog = new Promise(
function(resolve, reject) {
InsertCall(event.body, function(err, response) {
resolve({event: event, response: response});
}):
});
然后你可以得到解决的值;
...().then(function(resolveData) {
console.log(resolveData); // resolveData is { event: event, response: response }
return insertDataToLog();
})