0
var a = [0,1,2,3];

how do I pass the value of getVal to prod and tst.

    function startFunc(){
     var deferred = Q.resolve();

     var a = [0,1,2,3];
     a.forEach(function (num, i) {
       var getVal = num;
       deferred = deferred
          .then(function(num){
            var def = Q.defer();
            console.log("\n\niteration:"+i + " a: "+getVal);
            return def.resolve(getVal);
          }).then(prod)
          .then(tst)
          .then(compare)
          .catch(function(error){
            console.log(error);
          });
      });

      return deferred.promise;
    }

here is a nodejs fiddle link. Goto the link and execute press shift+enter to exec. https://tonicdev.com/pratikgala/5637ca07a6dfbf0c0043d7f9

When this is executed I want to pass the value of getVal to prod as a promise.

how do I do that. when I run the following function the getVal is not returend to prod.

patz
  • 1,306
  • 4
  • 25
  • 42
  • Your objective here isn't entirely clear. You're running a `.forEach()` loop that has async operations in it. Are you trying to run those async operations in parallel (all at the same time) or in series (one after the other)? Also, you have multiple values for getVal (one for each time through the loop). What exactly do you want to resolve as the final value of the `startFunc()` promise? Do you want the final value to be an array of promises? – jfriend00 Nov 02 '15 at 21:48
  • yes so here operations run in series, one after the other. and every time getVal gets a new value. that value is accessed inside the then function and returned with deferred.resolve(getVal). I want to use the getVal in prod() and test() function. I want the final value returned to be as a promise. – patz Nov 02 '15 at 21:54
  • @jfriend00 the problem is prod does not get the value. which is what I want to resolve. – patz Nov 02 '15 at 21:55
  • First of all, stop using the [deferred antipattern](http://stackoverflow.com/q/23803743/1048572) – Bergi Nov 02 '15 at 22:13

1 Answers1

0

You can significantly simplify your loop, stop using the deferred anti-pattern and get the val passed to prod() like this:

function startFunc() {
    var a = [0, 1, 2, 3];
    return a.reduce(function(p, val, i) {
        return p.then(function() {
            return prod(val);
        }).then(test).then(compare).catch(function (error) {
            console.log(error);
        });
    }, Q());
}    

startFunc().then(function(finalVal) {
    // completed successfully here
}, function(err) {
    // error here
});

This is designed to iterate through the items in the array, passing each one to prod() and to run a chain of promises on each item in the array, one after the other so that the whole promise chain for the first item in the array finishes before the next item in the array starts processing. The final value will be a promise that resolves to whatever the last step in the .reduce() loop returns. If you wish to accumulate an array of return values, that can be done also.

FYI, you can read about avoiding the deferred anti-pattern here.

jfriend00
  • 683,504
  • 96
  • 985
  • 979