Created
May 12, 2016 19:54
-
-
Save KKrisu/9ff30010fe39565abb17e92e6ef492d0 to your computer and use it in GitHub Desktop.
ES6 Promises
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
parent.console.clear(); | |
function asyncFunction(id = '', delay = 1000) { | |
return new Promise((resolve, reject) => { | |
setTimeout(() => { | |
// resolve('yeah ' + id); | |
reject('nooo ' + id); | |
}, delay); | |
}); | |
} | |
asyncFunction() | |
.then((result) => { | |
console.log('success', result); | |
return result; | |
}) | |
.catch((error) => { | |
console.error('catch', error); | |
// return 'yeah 2'; | |
throw 'kupa'; | |
}) | |
.then((result) => { | |
console.log('success', result); | |
}/*, (error) => { | |
console.error('error', error); | |
}*/) | |
.catch((error) => { | |
console.error('catch', error); | |
}); | |
// Promise.all([ | |
// asyncFunction(), | |
// asyncFunction(), | |
// ]) | |
// .then((result) => { | |
// console.log('success', result); | |
// }) | |
// .catch((error) => { | |
// console.error('catch', error); | |
// }); | |
Promise.race([ | |
asyncFunction(1, Math.random() * 1000), | |
asyncFunction(2, Math.random() * 1000), | |
]) | |
.then((result) => { | |
console.log('success', result); | |
}) | |
.catch((error) => { | |
console.error('catch', error); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment