Javascript promises result area

I need to link several asynchronous actions. I am trying to write Parse.com cloud code that uses express. (I think express is support for promises). I understand why promises are valuable, but I'm still not sure about two things. First, how to collect the results of sequential actions. Below is the code:

function doAsnychThingsInSequence(params) {
  return doThing0(params).then(function(resultOfThing0) {
    // thing1 depends on resultOfThing0
    doThing1(resultOfThing0);
  }).then(function(resultOfThing1) {
    // here is where I am confused.
    // thing2 depends on the results of thing0 and thing1
    doThing2(resultOfThing0 /* out of scope?? */, resultOfThing1);
  }, function(error) {
    // handle error
  });
}

After thing1 is done, I need the results of both actions. I think I can highlight the variable at the top of the function and assign it to the first result in the first callback, is that right? Perhaps the basis of my confusion is the second question ...

  return doThing0(params0).then(function(resultOfThing0) {
    doThing1(resultOfThing0);
    // what does return mean here?  does what I return here relate to the
    // parameters of the next function?
    return "foo";
  }).then(function(param) {
    // what is in param?  is it "foo"?
  }, function(error) {
});
+3
source share
3 answers

Promises : , - .

, ( ) , , , :

function doThing1(params1) {
  ...
  return [params1, result1];
}

function doThing2(params1, params2) {
  ...
}

, doThing1 doThing2, statefull, . .

+1

, then(), , , . ( promises) then() . , .

, 1 , . - :

// example
var thing1;
getThing1()
.then(function(value){
  thing1 = value; // <-- store in higher scope
  return getThing2(); // <-- return
})
.then(function(value){
  thing2 = value;
  // now you have both thing1 and thing2 in scope
})
+1

, @greim @c-smile. +1 . , Promises.

@greim . , . , , .

The @ c-smile answer will cause my functions to return an array (or some collection) of their parameters and their result. Then all the calling functions of this function will dig out the return value for the "natural" result, unlike the parameters that he used to get the result.

Repeating the answers in terms of my original post:

// @greim

function doAsnychThingsInSequence(params) {
  var theResultOfThing0;  // yuck, for needing to make up a non-colliding variable name
  return doThing0(params).then(function(resultOfThing0) {
    theResultOfThing0 = resultOfThing0; 
    return doThing1(resultOfThing0);
  }).then(function(resultOfThing1) {
    return doThing2(theResultOfThing0, resultOfThing1);
  }, function(error) {
    // handle error
  });
}

// @c-smile

function doThing1(params) {  // params are resultOfThing0
  // do stuff
  // the "natural" answer is just resultOfThing1, but ...
  return [params, resultOfThing1];
}

// if this function was doThingN, then we'd have
  return [params0, params1 ... paramsN, resultOfThingN];  // yikes!

// at least this method looks great now, how I want it to look...
function doAsnychThingsInSequence(params) {
  return doThing0(params).then(function(resultOfThing0) {
    return doThing1(resultOfThing0);
  }).then(function(resultOfThing0, resultOfThing1) {
    return doThing2(resultOfThing0, resultOfThing1);
  }, function(error) {
    // handle error
  });
}
+1
source

All Articles