Skip to content

Instantly share code, notes, and snippets.

@dominykas
Last active August 29, 2015 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dominykas/5f028aab59088291492c to your computer and use it in GitHub Desktop.
Save dominykas/5f028aab59088291492c to your computer and use it in GitHub Desktop.
You need the value of the first promise at the end of the chain - what do you do?
var p1value;
var final = promise1
.then(function (r) {
p1value = r;
return getPromise2(r);
})
.then(function (p2value) {
return finalResult(p1Value, p2Value)
});
var final = promise1
.then(function (p1value) {
return [p1value, getPromise2(p1value)]
})
.spread(function (p1value, p2value) {
// spread is a Q function - alternative is to do function (args) and expand manually
// careful: do other libraries (Promises/A+) expand an array to unwrap all promises?
return finalResult(p1value, p2value);
});
var final = promise1
.then(function (p1value) {
return getPromise2(p1value)
})
.then(function (p2value) {
return processValue(p2Value);
})
.then(function (processedValue) {
return [promise1, processedValue]; // use the promise - don't closure its value
});
.spread(function (p1value, processedValue) {
return finalResult(p1value, processedValue);
});
var promise2 = promise1
.then(function (p1value) {
return getPromise2(p1value);
});
var promise3 = promise1
.then(function (p1value) {
return getPromise3(p1value);
});
var final = Q.all([promise1, promise2, promise3])
.spread(function (p1value, p2value, p3value) {
return finalResult(p1Value, p2value, p3value);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment