Created
April 18, 2017 13:20
-
-
Save carlosvini/b21ba0988fa4827f3003c8518ab90af1 to your computer and use it in GitHub Desktop.
Javascript Aync/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
async function timeoutFunc(char, interval) { | |
console.log('timeoutFunc - '+char); | |
const promise = new Promise(function(resolve, reject) { | |
setTimeout(function () { | |
console.log('timeoutFunc - '+char+interval); | |
resolve(interval); | |
}, interval); | |
}); | |
return await promise; | |
} | |
async function asyncFunc(char) { | |
console.log('timeoutFunc'); | |
return await Promise.all([ | |
timeoutFunc(char, 100), | |
timeoutFunc(char, 200), | |
timeoutFunc(char, 300), | |
timeoutFunc(char, 400), | |
timeoutFunc(char, 500) | |
]); | |
} | |
async function doSomething() { | |
console.log('Do something'); | |
const results = await Promise.all([ | |
asyncFunc('A'), | |
asyncFunc('B'), | |
]); | |
console.log(results); | |
} | |
function run(){ | |
doSomething(); | |
console.log('After something'); | |
} | |
run(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment