Created
September 12, 2019 21:07
-
-
Save coolsoftwaretyler/3d7ebbf4cdb56f85486f3ef8ca463231 to your computer and use it in GitHub Desktop.
Await
This file contains 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
doSomeLongRunningTask(function(response, error) { | |
// This function is running as a CALLBACK inside the long task | |
// So it logs on completion | |
console.log(response) | |
// Return the response | |
return response; | |
} | |
var res = doSomeLongRunningTask(function(response, error) { | |
// You might expect response to get returned and assigned to `res` | |
// But since the task runs long, and you haven't asked to await it, your `res` var gets `undefined` | |
return response; | |
} | |
var res = await doSomeLongRunningTask(function(response, error) { | |
// As long as `doSomeLongRunningTask` is an async function, you can `await` it, and `res` will get assigned at the end of the long task | |
return response; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment