Created
May 13, 2019 13:01
-
-
Save lpeppe/5f35c5c653e159ae3f75d9ad2c355653 to your computer and use it in GitHub Desktop.
Promises examples
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 prom1 = new Promise((resolve, reject) => { | |
// resolve(2 + 2); | |
// }); | |
// const prom2 = new Promise((resolve, reject) => { | |
// setTimeout(() => resolve(1 + 1), 2000); | |
// }); | |
// // prom1.then(console.log); | |
// Promise.all([prom1, prom2]).then(console.log) | |
const fun1 = () => { | |
return new Promise((resolve, reject) => { | |
setTimeout(() => reject(10), 2000); | |
}); | |
}; | |
const fun2 = () => { | |
return new Promise((resolve, reject) => { | |
setTimeout(() => resolve(5), 2000); | |
}); | |
}; | |
// fun1() | |
// .then(() => { | |
// console.log("test"); | |
// return fun1(); | |
// }) | |
// .then(() => console.log("yo")); | |
const test = async () => { | |
try { | |
const num = await fun1(); | |
console.log("I'm here"); | |
await fun1(); | |
} catch (e) { | |
throw e; | |
} | |
}; | |
test().catch(console.error); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment