Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Javascript Async Await
function fetch(url, callback) {
console.log('Getting ' + url);
var delay = (Math.round(Math.random() * 1E4) % 4000) + 1000
var response = 'Content for ' + url;
setTimeout(function() {
callback(response)
}, delay);
}
function promiseFetch(url) {
return new Promise(function(resolve, reject) {
fetch(url, function(data) {
resolve(data);
})
})
}
async function main() {
var data1 = await promiseFetch('file1');
var data2 = await promiseFetch('file2');
var data3 = await promiseFetch('file3');
var data = JSON.parse( result1 );
console.log(data1, data2, data3)
}
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.