0

can I keep the code clean like that and fetch -paramFromA & paramFromB? all of the functions here return new Promise

  var a = helper.getAccountPromise(tokens);
  var b = a.then(helper.getFundingPromise)
  var c = b.then(helper.createCampignPromise(paramFromA, paramFromB))

UPDATE: let's say I'll do

 var a = helper.getAccountPromise(tokens);
 var b = a.then(helper.getFundingPromise)
 var c = helper.createCampignPromise(a, b))

....
createCapmaignPromise(a,b){
  // do I wait for a here? a.then ?
  // how do I extract the response here - console.log(a.response) ?

// same for b 

}

user1025852
  • 2,684
  • 11
  • 36
  • 58
  • You mean, if it will work? Yes, it will work. The `then` handlers return a new promise object. – thefourtheye Jun 25 '16 at 17:33
  • paramFromA & paramFromB are meaningless names here :) (paramFromA is not ddefined...) how do I actually extract value from the result of A and value from the result of B? this one I can't figure out – user1025852 Jun 25 '16 at 17:39
  • Pass those promises to `createCampignPromise` and when both of them resolve, get the value and use them – thefourtheye Jun 25 '16 at 17:41

1 Answers1

0

.then() is always called with one argument, which is the value the promise was resolved with.

I assume paramFromA and paramFromB mean the return values from Promises "a" and "b", so in your chain "c"'s then will only have access to paramFromB.

You could var c = Promise.all(a, b).then(/* two item array is passed as single argument here */) In promise libraries like bluebird, you have a .spread() method instead of .then() that spreads the array returned from Promise.all() into two arguments, but you can also use the ES6 ... spread operator.

If you need the promises to run in sequence and pass the return value from a to c, you either have to pass it along from b or store it in a variable outside of the closure in .then();

marton
  • 1,300
  • 8
  • 16