Created
May 14, 2019 21:14
-
-
Save pinei/e85da63fe58b0913c689a4a156bb4e7e to your computer and use it in GitHub Desktop.
Javascript async/await
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
| const LIST = ['Orange', 'Lemon', 'Banana' ] | |
| process(LIST); | |
| async function eachAsync(arr, fn) { // take an array and a function | |
| for(const item of arr) await fn(item); | |
| } | |
| async function process(list) { | |
| await eachAsync(list, async (data) => { | |
| let result = await print(data) | |
| console.log(result) | |
| }); | |
| console.log('Done'); | |
| } | |
| function print(data) { | |
| return new Promise((resolve, reject) => { | |
| setTimeout(() => { | |
| console.log(data); | |
| resolve("Data was sent") | |
| }, 3000) | |
| }); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment