Skip to content

Instantly share code, notes, and snippets.

@craigvantonder
Last active August 24, 2021 05:42
Show Gist options
  • Save craigvantonder/5936ae6dfef7835d6762b41ebb53be01 to your computer and use it in GitHub Desktop.
Save craigvantonder/5936ae6dfef7835d6762b41ebb53be01 to your computer and use it in GitHub Desktop.
Async function handling examples
// https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Asynchronous/Introducing
var inputString = 'Hello World';
function callbackStyleFunction (input, cb) {
//if (someErrorCondition) return cb('something bad happened');
cb(null, input);
};
const promiseStyleFunction = function (input) {
return new Promise(function (resolve, reject) {
//if (someErrorCondition) return reject('something bad happened')
resolve(input)
})
}
// Example 1 - callback
callbackStyleFunction(inputString, function (errorFromCallback, resultOfCallback) {
console.log('callback finished');
console.log(errorFromCallback);
console.log(resultOfCallback+' from callback');
// Example 2 - chained promises (this will fire pretty much at the same time as example 3)
const { promisify } = require('util')
const returnInput = promisify(callbackStyleFunction)
returnInput(inputString)
.then(function (resultOfCallbackAsPromise) {
console.log('do something else after')
console.log(resultOfCallbackAsPromise+' from 1st chained function')
return resultOfCallbackAsPromise
})
.then(async function (resultOfCallbackAsPromise) {
console.log('finally do something here')
console.log(resultOfCallbackAsPromise+' from 2nd chained function')
const resultOfPromise = await promiseStyleFunction(inputString)
console.log(resultOfPromise+' from promise awaited in the async function that is the 2nd chained function')
})
.catch(function (errorFromCallbackAsPromise) {
console.log('whoops, chain of promises broke')
console.log(errorFromCallbackAsPromise)
})
// Example 3 - async / await the callback function (this will fire pretty much the same time as example 2, just after it in reality)
;/*here be dragons*/(async function () {
try {
var resultOfCallbackAsPromise = await returnInput(inputString)
console.log('a bit easier')
console.log(resultOfCallbackAsPromise+' from callback converted to promise and awaited in an async IIFE')
const resultOfPromise = await promiseStyleFunction(inputString)
console.log(resultOfPromise+' from promise awaited in an async IIFE')
} catch (errorFromCallbackAsPromise) {
console.log('whoops, async did not await the callback function')
console.log(errorFromCallbackAsPromise)
}
})()
})
// Example 4 - async / await the promise function (this will fire pretty much the same time as example 1, just after it in reality)
async function showTheInput () {
try {
const resultOfPromise = await promiseStyleFunction(inputString)
console.log('a bit more easier')
console.log(resultOfPromise+' from promise awaited in an async function')
} catch (errorFromPromise) {
console.log('whoops, async did not await the promise function')
console.log(errorFromPromise)
}
}
showTheInput()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment