Skip to content

Instantly share code, notes, and snippets.

@luisenriquecorona
Last active May 15, 2019 17:56
Show Gist options
  • Save luisenriquecorona/f8aedc3a918dcf81eef3db5e42915be5 to your computer and use it in GitHub Desktop.
Save luisenriquecorona/f8aedc3a918dcf81eef3db5e42915be5 to your computer and use it in GitHub Desktop.
Simple Asynchronous Functions
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