复杂的承诺和循环

时间:2018-08-16 21:09:43

标签: javascript node.js for-loop promise resolve

一段时间以来,我一直在为复杂的妆容而苦苦挣扎。我无法使用async / await,因为azure函数v1不支持它。

问题如下...

我得到了一个数组allArray

现在我想完成如下所示的操作,但是我似乎很难过。

编辑: 问题似乎是该函数到达了loop并通过for循环发送了allArray[0],而我又得到了context.log("One: " + results[0], "Two: " + results[1]);,但它并没有等待发送空的anotherArray,它也不会启动var call也不进行下一个循环。

我的日志如下:

no thanks (oneAnalysis)
No things (twoAnalysis)
Done again
[]
Done
One: " + results[0], "Two: " + results[1]
Function completed

任何帮助将不胜感激。

module.exports = function (context, allArray) {
    var loopPromise = loop(context, allArray);
        Promise.all([loopPromise]).then(function(results){ 
            context.log('Done again');
            context.log(results[0]);
            context.done() 
        });
}

function loop (context, allArray) {
    var anotherArray = [];
    for (i = 0; i < allArray.length; i++) {
        var promiseOne = oneAnalysis (context, allArray[i]);
        var promiseTwo = twoAnalysis (context, allArray[i]); 

        Promise.all([promiseOne, promiseTwo]).then(function(results){ 
            context.log('Done');
            context.log("One: " + results[0], "Two: " + results[1]);

            var Id = allArray[i].Id;
            var call = callCF (context, Id)
            anotherArray.push(call);
        });
    }
    return Promise.all(anotherArray);
}

function oneAnalysis (context, input) {

    if (input.something.length > 0) {
        var somethingArray = [];
        for (i = 0; i < input.something.length; i++) {
            if (stuff) {
                var queue = queue;
                var text = {
               text                
}
                var create = createMessage(text, queue, context);
                    somethingArray.push(create);
            } else {
                somethingArray.push("no thanks");
            }
        }
        return Promise.all(somethingArray);
    } else {
        return ("No things");
    }
}

function twoAnalysis (context, input) {
same as oneAnalysis
}

function createMessage(text, queue, context) {
    return new Promise((resolve, reject) => {

        queueService.createMessage(queue, JSON.stringify(text), function(error) {
            if (!error) {
                context.log('Message inserted:', text);
                resolve ('Message inserted: ' + text)
            }
            else {
                context.log('All done'); 
                resolve ('error');
            }
        });
    });
}

function callCF(context, id) {
    return new Promise((resolve, reject) => {
        Do http request and resolve on end
    });
}

2 个答案:

答案 0 :(得分:2)

您的loop方法对我来说似乎是有问题的,您在执行anotherArray之前不必等待循环中生成的promise解析,这很可能就是您看到奇怪行为的原因。

您应重新构造方法,以使其在尝试解决anotherArray之前正确解决旨在影响anotherArray的承诺。

function loop (context, allArray) {
    var anotherArray = [];

    var processing = allArray.map(function(elem) {
        var promiseOne = oneAnalysis(context, elem);
        var promiseTwo = twoAnalysis(context, elem);
        return Promise.all([promiseOne, promiseTwo]).then(function(results){
            context.log('Done');
            context.log("One: " + results[0], "Two: " + results[1]);

            var Id = elem.Id;
            var call = callCF(context, Id)
            anotherArray.push(call);
        });
    });

    return Promise.all(processing).then(function() {
        return Promise.all(anotherArray);
    });
}

答案 1 :(得分:1)

async / await很不错,但最终它们只是常规承诺的基础。正如我在对您的问题的评论中提到的那样,据我了解,我正在对您的伪代码和重构代码进行一些假设。

由于您可以使用promises,因此我假设您也可以使用const和arrow函数;如果不是这种情况,请告诉我,然后我可以重构代码。

module.exports = (context, allArray) => {
  // loop returns a promise already from Promise.all so we do not need to wrap
  // it in another promise.
  loop(context, allArray).then(results => {
    context.log('Done again');
    context.log(results[0]);
    context.done()
  });
}

function loop(context, allArray) {
  // this reduce will take every item in `allArray`
  const anotherArray = allArray.reduce((accumulator, item) => {
    // For each item in anotherArray, call oneAnalysis and twoAnalysis; return 
    // values are promises and we need to wait for them to resolve using 
    // Promise.all.
    const promiseOne = oneAnalysis(context, item);
    const promiseTwo = twoAnalysis(context, item);
    const callCFPromise = Promise.all([promiseOne, promiseTwo]).then(results => {
      context.log('Done');
      context.log('One: ' + results[0], 'Two: ' + results[1]);

      const Id = allArray[i].Id;

      // Once promiseOne and promiseTwo are resolved call callCF which returns
      // another promise itself. This is the final promised to be returned to
      // callCFPromise.
      return callCF(context, Id);
    });

    // callCFPromise will be a promise to be resolved with the value of callCF 
    // after promiseOne and promiseTwo are resolved. We are inside a reduce so
    // we need to push our promise in the accumulator and return it for the next
    // iteration.
    accumulator.push(callCFPromise);

    return accumulator;
  }, []);

  return Promise.all(anotherArray);
}

function oneAnalysis(context, input) {
  if (!input.something.length) {
    return 'No things';
  }

  // Take every item in `input.something`, and if `stuff` is truthy, it will 
  // call createMessage, and push the promise to accumulator. in the end, the 
  // resulting accumulator (array of promises) will be returned. In the original
  // version of this code I did not see any reference input.something[i] so in
  // this case I am not doing anything to item, but I guess it will be used for
  // something.
  const createPromises = input.something.reduce((accumulator, item) => {
    if (!stuff) {
      photoArray.push('no thanks');
    } else {
      // I am guessing these queue and text have something to do with item (which
      // is input.something[i] in the original shared code)
      const queue = queue;
      const text = { text };
      const create = createMessage(text, queue, context);

      // Since we are in a reduce, we need to push our create promise into the
      // accumulator and return it for the next iteration. In this case we only
      // push the promises we create instead of every iteration.
      accumulator.push(create);
    }

    return accumulator;
  }, []);

  // Take the resulting accumulator of promises and create a new promise that
  // resolves when every promise in the array is resolved.
  return Promise.all(createPromises);
}

function twoAnalysis(context, input) {
  // same as oneAnalysis
}

function createMessage(text, queue, context) {
  return new Promise((resolve, reject) => {
    queueService.createMessage(queue, JSON.stringify(text), error => {
      if (error) {
        return reject(error);
      }

      context.log('Message inserted:', text);
      return resolve('Message inserted: ' + text);
    });
  });
}

function callCF(context, id) {
  return new Promise((resolve, reject) => {
    // Do http request and resolve on end
  });
}