Skip to content

Instantly share code, notes, and snippets.

@matthewmueller matthewmueller/promise.js
Last active Aug 29, 2015

Embed
What would you like to do?
Runthrough of Promise control flow
var a = new Promise(function (resolve, reject) {
reject(1)
})
function b_success() {
console.log('b: success', err);
// not called...
return new Promise(function (resolve, reject) {
reject(2)
})
}
function b_fail(err) {
console.log('b: fail', err);
throw err;
}
function c_success(value) {
// called
console.log('c: success', value);
}
function c_fail(err) {
// not called
console.log('c: fail', err);
return err
}
function d_success(value) {
// called
console.log('d: success', value);
// new promise will update the success value
// when it completes, vs. just returning value
return new Promise(function(resolve, reject) {
resolve(2);
})
}
function d_fail(err) {
// not called
console.log('d: fail', err);
}
function e_success(value) {
// called
console.log('e: success', value);
}
function e_fail(err) {
// not called
console.log('e: fail', err);
}
a
.then(b_success, b_fail)
.then(c_success, c_fail)
.then(d_success, d_fail)
.then(e_success, e_fail)
// Yields:
//
// b: fail 1
// c: fail 1
// d: success 1
// e: success 2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.