Last active
May 15, 2019 17:56
-
-
Save luisenriquecorona/f8aedc3a918dcf81eef3db5e42915be5 to your computer and use it in GitHub Desktop.
Simple Asynchronous Functions
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
let getDataOne = (cb) => { | |
setTimeout(function(){ | |
//calling the callback | |
cb('dummy data one') | |
}, 1000); | |
} | |
let getDataTwo = (cb) => { | |
setTimeout(function(){ | |
//calling the callback | |
cb('dummy data two') | |
}, 1000); | |
} | |
//Both these functions mimic the async code with setTimeout. | |
//Once the desired time has elapsed, setTimeout will call the | |
//passed callback cb with value dummy data one and dummy data two, | |
//respectively. Let’s see how we will be calling these two functions | |
//without generators in the first place: | |
getDataOne((data) => console.log("data received",data)) | |
getDataTwo((data) => console.log("data received",data)) | |
//That code will print the following after 1,000 ms: | |
//Result Running | |
data received dummy data one | |
data received dummy data two |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment