Created
March 13, 2020 13:35
-
-
Save jgfrancisco/25d8ad24735333ad3d412f865c53e2a6 to your computer and use it in GitHub Desktop.
Promise short circuit behavior
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
const fn1 = (a, b, x) => new Promise((resolve, reject) => { | |
if (x) resolve('resolve x'); | |
console.log('after x'); | |
if (a) reject('reject a'); | |
console.log('after a'); | |
if (b) reject('reject b'); | |
console.log('after b'); | |
resolve('resolve c'); | |
}); | |
const fn2 = (a, b, x) => new Promise((resolve, reject) => { | |
if (x) return resolve('resolve x'); | |
console.log('after x'); | |
if (a) return reject('reject a'); | |
console.log('after a'); | |
if (b) return reject('reject b'); | |
console.log('after b'); | |
resolve('resolve c'); | |
}); | |
console.log('<< fn1 >>'); | |
fn1(1, 1, 1) | |
.then(ret => console.log(ret)) | |
.catch(err => console.log(err)); | |
console.log('<< fn2 >>'); | |
fn2(1, 1, 1) | |
.then(ret => console.log(ret)) | |
.catch(err => console.log(err)); | |
console.log('hello world'); |
Author
jgfrancisco
commented
Mar 13, 2020
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment