如何在 javascript 中创建嵌套承诺?
How to create nested promises in javascript?
我正在使用普通 javascript。对于数组的每个元素,我有 3 个任务要做。我为每个元素创建了承诺,每个元素都承诺为每个元素完成任务。现在在每个里面,我想做出 3 个承诺,每个任务一个。
processElement=processArrayElementFunction(matrix);
unique.forEach(function (number,index)
{
promises.push(new promiseToProcessElement(index,number,processElement,matrix));
});
Promise.all(promises).then((results) => {console.log(results);});
function promiseToProcessElement(id,num,callbackProcessElement,matrix)
{
return new Promise((resolve, reject) => {
resolve(callbackProcessElement(id, num,matrix););
});
}
function processArrayElementFunction(matrix)
{
return function(index, number)
{
var promises=[];
promises.push(new promiseTask(index,sumRC,matrix));
promises.push(new promiseTask(index,sumAround,matrix));
promises.push(new promiseTask(number,repetitions,matrix));
Promise.all(promises).then((results) => {
return results;
});
};
}
function promiseToProcessElement(id,num,callbackProcessElement,matrix)
{
return new Promise((resolve, reject) => {
resolve(callbackProcessElement(id, num,matrix););
});
}
function promiseTask(num,callbackTask,matrix)
{
return new Promise((resolve,reject)=>
{
resolve(callbackTask(num,matrix));
});
}
sumRC、sumAround、repetitions 只是执行任务的一些函数。它们并不重要。
现在 function promiseToProcessElement
中的 var result=callbackProcessElement(id, num,matrix);
是 undefined
。
我认为问题是,因为程序需要这个结果,而没有完成每个元素的 3 个任务。这是真的?我该如何解决?
你的问题出在这两个函数上,我添加了注释来标记它们:
function promiseToProcessElement(id,num,callbackProcessElement,matrix)
{
return new Promise((resolve, reject) => {
var result=callbackProcessElement(id, num,matrix);
resolve(result);//You resolve the promise without waiting.
});
}
function processArrayElementFunction(matrix)
{
return function(index, number)
{
var promises=[];
promises.push(new promiseTask(index,sumRC,matrix));
promises.push(new promiseTask(index,sumAround,matrix));
promises.push(new promiseTask(number,repetitions,matrix));
// You do not return anything.
Promise.all(promises).then((results) => {
return {Elementi: index+1,ShumaRreshtKolone:results[0],ShumaNrRrethues:results[1],sumAround:results[2]};
}
).catch(function (error){console.log(..);});
};
}
你白白创造了太多承诺...你可以保留它们,也可以简化它们,这取决于你,但我会按照以下方式解决它。
function promiseToProcessElement(id,num,callbackProcessElement,matrix)
{
//Do not create another promise, just return the one that is created for the tasks.
return callbackProcessElement(id, num,matrix);
}
function processArrayElementFunction(matrix)
{
return function(index, number)
{
var promises=[];
promises.push(new promiseTask(index,sumRC,matrix));
promises.push(new promiseTask(index,sumAround,matrix));
promises.push(new promiseTask(number,repetitions,matrix));
// Return the promise so we can chain.
return Promise.all(promises).then((results) => {
return {Elementi: index+1,ShumaRreshtKolone:results[0],ShumaNrRrethues:results[1],sumAround:results[2]};
}
).catch(function (error){console.log(..);});
};
}
我认为这会起作用,但很难说,因为您的代码没有精简到最低限度,而且它似乎使您的任务过于复杂。
我认为它应该是这样的。
unique.forEach(function (number,index)
{
promises.push(doTask(number,matrix));
Promise.all(promises).then((results) => {console.log(results);}
).catch(function (error){...);});
function doTask(number,matrix){
let proms = [];
proms.push(new Promise(function(done){
sumRC(number,matrix,done);
}));
proms.push(new Promise(function(done){
sumAround(number,matrix,done);
}));
proms.push(new Promise(function(done){
repetitions(number,matrix,done);
}));
return Promise.all(proms).then(function(results){
return {Elementi: index+1,ShumaRreshtKolone:results[0],ShumaNrRrethues:results[1],sumAround:results[2]};
});
}
//Example of how sumRC should look
function sumRC(number,matrix,done){
//DO SUM or whatever
var result = number+1;
done(result);
}
主要问题是您在提供的代码中没有任何充分理由地使用 promises,所以我假设 sumRC 是一个异步函数,它在末尾有一个回调函数,它意味着什么。
确保 processArrayElementFunction
returns Promise.all
function processArrayElementFunction(matrix)
{
return function(index, number)
{
var promises=[];
promises.push(new promiseTask(index,sumRC,matrix));
promises.push(new promiseTask(index,sumAround,matrix));
promises.push(new promiseTask(number,repetitions,matrix));
RETURN Promise.all(promises).then((results) => {
return {Elementi: index+1,ShumaRreshtKolone:results[0],ShumaNrRrethues:results[1],sumAround:results[2]};
}
).catch(function (error)
{
console.log(..);});
};
}
我正在使用普通 javascript。对于数组的每个元素,我有 3 个任务要做。我为每个元素创建了承诺,每个元素都承诺为每个元素完成任务。现在在每个里面,我想做出 3 个承诺,每个任务一个。
processElement=processArrayElementFunction(matrix);
unique.forEach(function (number,index)
{
promises.push(new promiseToProcessElement(index,number,processElement,matrix));
});
Promise.all(promises).then((results) => {console.log(results);});
function promiseToProcessElement(id,num,callbackProcessElement,matrix)
{
return new Promise((resolve, reject) => {
resolve(callbackProcessElement(id, num,matrix););
});
}
function processArrayElementFunction(matrix)
{
return function(index, number)
{
var promises=[];
promises.push(new promiseTask(index,sumRC,matrix));
promises.push(new promiseTask(index,sumAround,matrix));
promises.push(new promiseTask(number,repetitions,matrix));
Promise.all(promises).then((results) => {
return results;
});
};
}
function promiseToProcessElement(id,num,callbackProcessElement,matrix)
{
return new Promise((resolve, reject) => {
resolve(callbackProcessElement(id, num,matrix););
});
}
function promiseTask(num,callbackTask,matrix)
{
return new Promise((resolve,reject)=>
{
resolve(callbackTask(num,matrix));
});
}
sumRC、sumAround、repetitions 只是执行任务的一些函数。它们并不重要。
现在 function promiseToProcessElement
中的 var result=callbackProcessElement(id, num,matrix);
是 undefined
。
我认为问题是,因为程序需要这个结果,而没有完成每个元素的 3 个任务。这是真的?我该如何解决?
你的问题出在这两个函数上,我添加了注释来标记它们:
function promiseToProcessElement(id,num,callbackProcessElement,matrix)
{
return new Promise((resolve, reject) => {
var result=callbackProcessElement(id, num,matrix);
resolve(result);//You resolve the promise without waiting.
});
}
function processArrayElementFunction(matrix)
{
return function(index, number)
{
var promises=[];
promises.push(new promiseTask(index,sumRC,matrix));
promises.push(new promiseTask(index,sumAround,matrix));
promises.push(new promiseTask(number,repetitions,matrix));
// You do not return anything.
Promise.all(promises).then((results) => {
return {Elementi: index+1,ShumaRreshtKolone:results[0],ShumaNrRrethues:results[1],sumAround:results[2]};
}
).catch(function (error){console.log(..);});
};
}
你白白创造了太多承诺...你可以保留它们,也可以简化它们,这取决于你,但我会按照以下方式解决它。
function promiseToProcessElement(id,num,callbackProcessElement,matrix)
{
//Do not create another promise, just return the one that is created for the tasks.
return callbackProcessElement(id, num,matrix);
}
function processArrayElementFunction(matrix)
{
return function(index, number)
{
var promises=[];
promises.push(new promiseTask(index,sumRC,matrix));
promises.push(new promiseTask(index,sumAround,matrix));
promises.push(new promiseTask(number,repetitions,matrix));
// Return the promise so we can chain.
return Promise.all(promises).then((results) => {
return {Elementi: index+1,ShumaRreshtKolone:results[0],ShumaNrRrethues:results[1],sumAround:results[2]};
}
).catch(function (error){console.log(..);});
};
}
我认为这会起作用,但很难说,因为您的代码没有精简到最低限度,而且它似乎使您的任务过于复杂。
我认为它应该是这样的。
unique.forEach(function (number,index)
{
promises.push(doTask(number,matrix));
Promise.all(promises).then((results) => {console.log(results);}
).catch(function (error){...);});
function doTask(number,matrix){
let proms = [];
proms.push(new Promise(function(done){
sumRC(number,matrix,done);
}));
proms.push(new Promise(function(done){
sumAround(number,matrix,done);
}));
proms.push(new Promise(function(done){
repetitions(number,matrix,done);
}));
return Promise.all(proms).then(function(results){
return {Elementi: index+1,ShumaRreshtKolone:results[0],ShumaNrRrethues:results[1],sumAround:results[2]};
});
}
//Example of how sumRC should look
function sumRC(number,matrix,done){
//DO SUM or whatever
var result = number+1;
done(result);
}
主要问题是您在提供的代码中没有任何充分理由地使用 promises,所以我假设 sumRC 是一个异步函数,它在末尾有一个回调函数,它意味着什么。
确保 processArrayElementFunction
returns Promise.all
function processArrayElementFunction(matrix)
{
return function(index, number)
{
var promises=[];
promises.push(new promiseTask(index,sumRC,matrix));
promises.push(new promiseTask(index,sumAround,matrix));
promises.push(new promiseTask(number,repetitions,matrix));
RETURN Promise.all(promises).then((results) => {
return {Elementi: index+1,ShumaRreshtKolone:results[0],ShumaNrRrethues:results[1],sumAround:results[2]};
}
).catch(function (error)
{
console.log(..);});
};
}