Skip to content

Instantly share code, notes, and snippets.

@KKrisu
Created May 12, 2016 19:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KKrisu/9ff30010fe39565abb17e92e6ef492d0 to your computer and use it in GitHub Desktop.
Save KKrisu/9ff30010fe39565abb17e92e6ef492d0 to your computer and use it in GitHub Desktop.
ES6 Promises
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