Created
February 26, 2019 21:19
-
-
Save d0rsha/3c3d77c8709abeea48b2225dfb06765f to your computer and use it in GitHub Desktop.
[Async] Async await over promises
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 random = () => { return Promise.resolve(Math.random()); } | |
// Callback hell | |
const sum randomAsyncAwait = () => { | |
let first; | |
let second; | |
let third; | |
return random.then(val => { | |
first = val; | |
return random(); | |
}).then(val => { | |
second = val; | |
return random(); | |
}).then(val => { | |
third = val; | |
return first + second + third; | |
}); | |
} | |
// Beytter | |
const randomAsyncAwait = async () => { | |
const first = await random(); | |
const second = await random(); | |
const third = await random(); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment