【问题标题】:How to create nested promises in javascript?如何在 javascript 中创建嵌套的 Promise?
【发布时间】:2017-07-24 16:53:24
【问题描述】:

我正在使用纯 JavaScript。对于数组的每个元素,我有 3 个任务要做。我为每个元素创建了 Promise,每个元素都承诺为每个元素完成任务。现在在每一个里面,我要做出 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 个任务。这是真的?我该如何解决?

【问题讨论】:

  • 您介意剥离您的代码吗?它很难阅读并且缺少一些大括号。
  • 我建议您更仔细地阅读有关 Promise 的内容...您所有的 Promise 都会同步调用 resolve...您可以将它们全部删除,它会做同样的事情。您需要使用 then 并返回 Promise,以便您可以链接。
  • @TheRickest 我剥离了代码。你能帮我吗?
  • @Salketer 你有什么建议?

标签: javascript nested asynccallback


【解决方案1】:

你的问题在于这两个函数,我已经添加了 cmets 来标记它们:

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

主要问题是您在提供的代码中使用了没有任何充分理由的承诺,所以我假设 sumRC 是一个异步函数,最后有一个回调来表示它的含义。

【讨论】:

  • 其实,没那么简单……重读你的代码,好像你误用了你所有的函数返回值。
  • 我应该如何组织?我之前没有使用过承诺
  • 首先将您的代码减少到最少......有3个函数只返回一些东西而不做任何事情。你可以完全摆脱它们。
  • 你能帮我吗?除了 processArrayElementFunction,我看不到,我应该做什么?
  • 您的解决方案显示此Promise {[[PromiseStatus]]: "pending", [[PromiseValue]]: undefined}__proto__: Promise[[PromiseStatus]]: "resolved"[[PromiseValue]]: Object
【解决方案2】:

确保processArrayElementFunction 返回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(..);});
    };
}

【讨论】:

  • 它是这样写的:Promise {[[PromiseStatus]]: "pending", [[PromiseValue]]: undefined}__proto__: Promise[[PromiseStatus]]: "resolved"[[PromiseValue]]: Object 但它最终会产生一个结果。
猜你喜欢
  • 2017-11-16
  • 2017-12-24
  • 2019-10-21
  • 1970-01-01
  • 2021-06-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多