Callbacks versus Promises versus async
As a refresher on the techniques that we have explored in this chapter, let's compare the techniques used when using callbacks, Promises, and async await all in one go. First up, the callback syntax is as follows:
function usingCallbacks() {
function afterCallbackSuccess() {
// execute when the callback succeeds
}
function afterCallbackFailure() {
// execute when the callback fails
}
// call a function and provide both callbacks
invokeAsync(afterCallbackSuccess, afterCallbackFailure);
// code here does not wait for callback to execute
}
Here, we have defined a function named usingCallbacks
. Within this function, we have defined two more functions, named afterCallbackSuccess
and afterCallbackFailure
. We then call an asynchronous function named invokeAsync
and pass in these two functions as arguments. One of these functions will be invoked by the asynchronous code, depending on whether the...