Last active
August 29, 2015 14:05
-
-
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?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var p1value; | |
var final = promise1 | |
.then(function (r) { | |
p1value = r; | |
return getPromise2(r); | |
}) | |
.then(function (p2value) { | |
return finalResult(p1Value, p2Value) | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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