javascript - 如何在 JavaScript 中创建嵌套的 Promise?

标签 javascript nested asynccallback

我正在使用纯 JavaScript。我要为数组的每个元素执行 3 项任务。我为每个元素创建了 promise ,其中每个元素都 promise 为每个元素执行任务。现在,我想在每个 promise 中做出 3 个 promise ,每个 promise 对应一个任务。

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 只是执行任务的一些函数。它们并不重要。

现在,函数promiseToProcessElement中的var result=callbackProcessElement(id, num,matrix);未定义。 我认为问题在于,程序要求这个结果,而没有完成每个元素的 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(..);});

   };

}

你毫无意义地创造了太多的 promise ...你可以保留它们,或者简化它们,这取决于你,但我将如何解决它。

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);
}

主要问题是您在提供的代码中没有任何充分理由地使用了 Promise,因此我假设 sumRC 是一个异步函数,它在末尾有一个回调,使其具有某种含义。

关于javascript - 如何在 JavaScript 中创建嵌套的 Promise?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45275175/

相关文章:

javascript - 隐藏输入字段的数组

javascript - 如何识别 jQuery 中箭头键的长按

sql - 通过 BigQuery 中的联接构建多级嵌套结构(使用嵌套和重复字段)

c# - 如何为 Control.BeginInvoke 委托(delegate) AsyncCallback 方法? (。网)

javascript - NodeJS Async - 将参数传递给回调

javascript - HTML javascript 动态调整表格大小

javascript - <a> 在 <div> 中不起作用

Javascript:从另一个对象在javascript中创建一个深度嵌套的对象

ios - 如何在 Swift 3 中访问深层嵌套的 NSDictionary?以前我们曾经这样访问 -

Javascript:在更改之前保留变量值