Skip to content

Instantly share code, notes, and snippets.

@coolsoftwaretyler
Created September 12, 2019 21:07
Show Gist options
  • Save coolsoftwaretyler/3d7ebbf4cdb56f85486f3ef8ca463231 to your computer and use it in GitHub Desktop.
Save coolsoftwaretyler/3d7ebbf4cdb56f85486f3ef8ca463231 to your computer and use it in GitHub Desktop.
Await
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