Created
February 21, 2017 21:23
-
-
Save whistler/a0b2eab7d1d5f1d9436b45dd37336658 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
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