Skip to content

Instantly share code, notes, and snippets.

@whistler
Created February 21, 2017 21:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save whistler/a0b2eab7d1d5f1d9436b45dd37336658 to your computer and use it in GitHub Desktop.
Save whistler/a0b2eab7d1d5f1d9436b45dd37336658 to your computer and use it in GitHub Desktop.
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