Skip to content

Instantly share code, notes, and snippets.

@jmartin-sul
Created May 18, 2019 14:46
Show Gist options
  • Save jmartin-sul/30c34b88e53abf1fe0c4ebd5d9e95cad to your computer and use it in GitHub Desktop.
Save jmartin-sul/30c34b88e53abf1fe0c4ebd5d9e95cad to your computer and use it in GitHub Desktop.
// you'll want to run the below manually by separately pasting each chunk into e.g. a node console
// declare some globals that we can use to get insight into the workings of slowFunction
preAwaitDate = null
postAwaitDate = null
awaitResult = null
slowFunction = async () => {
preAwaitDate = new Date()
awaitResult = await new Promise(function(resolve, reject) {
setTimeout(function() { resolve('all done!'); }, 30000);
})
postAwaitDate = new Date()
// return awaitResult // NOTE: now this doesn't return anything, so result of slowFunction should be a promise that resolves to undefined
}
// run these contiguous lines all at once
preAwaitDate = null
postAwaitDate = null
awaitResult = null
slowFunctionResult = null
console.log("\n###print initialized vars before slow async fn call###")
preAwaitDate
postAwaitDate
awaitResult
console.log(`\n###right before slow async fn call ${new Date()}###`)
slowFunctionResult = slowFunction()
console.log(`###right after slow async fn call ${new Date()}###\n`)
slowFunctionResult
preAwaitDate
postAwaitDate
awaitResult
// run again, but wait till after the timeout has passed
slowFunctionResult
preAwaitDate
postAwaitDate
awaitBlockingTime = postAwaitDate - preAwaitDate
awaitResult
typeof awaitResult
awaitResult.constructor.name
slowFunctionResult.constructor.name
typeof slowFunction
slowFunction.constructor.name
// for comparison
foo = function() { return 'bar' }
typeof foo
foo.constructor.name
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment