Created
July 26, 2017 07:09
-
-
Save ethaizone/aba892a997b712782901744233456d93 to your computer and use it in GitHub Desktop.
How to use await with promise. Javascript.
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
// USE BABEL WITH PRESET ES2017 | |
// FUCKING AWESOME PROMISE EXAMPLE | |
// FIRST PROMISE | |
function a() { | |
return Promise.resolve([1, 2, 3, 4, 5, 6]) | |
// return Promise.reject(new Error("xxxx")) | |
} | |
// WE CAN USE AWAIT UNDER ASYNC FUNCTION ONLY | |
let b = async () => { | |
try { | |
let res = await a() | |
console.log(res); // This will show array. | |
} | |
catch (err) { | |
console.log(err); | |
} | |
} | |
console.log(b()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment