Last active
August 29, 2015 14:27
-
-
Save matthewmueller/b8a0bf4ab5183243cde6 to your computer and use it in GitHub Desktop.
Runthrough of Promise control flow
This file contains 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 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