Skip to content

Instantly share code, notes, and snippets.

@pinei
Created May 14, 2019 21:14
Show Gist options
  • Select an option

  • Save pinei/e85da63fe58b0913c689a4a156bb4e7e to your computer and use it in GitHub Desktop.

Select an option

Save pinei/e85da63fe58b0913c689a4a156bb4e7e to your computer and use it in GitHub Desktop.
Javascript async/await
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