asyncAdapter
Take functions with a callback pattern and use them with async/await
!
The Problem
Let's say you have a legacy function that creates an XMLHttpRequest
which passes response data to a callback function, for example:
{ var xhr = ; xhr; xhr; xhr;}
It has been a trusty sidekick, but it is a little outdated and makes using retrieved data more involved than modern Javascript needs to be.
You want to use the latest and greatest APIs that tc39 and Babel can provide– like async/await
or the Promise
API– and callbacks just don't cut it.
What could you do?
The Solution
Enter asyncAdapter
. This nifty utility magically makes the function into a new Promise
-based function, allowing it to be await
-ed or otherwise handled like a Promise; this is achieved by passing in the Promise's resolve
argument where the original function's callback would go.
(Okay so it is not exactly magic, but it's still pretty cool)
Here is how you would use the example function above with asyncAdapter
:
const asyncHttpGET = ; { const data = await asyncHttpGET; console; // -> { foo: 'bar' }}
The first argument to the adapter is the original function name, and the rest of the arguments constitute any arguments you would pass to the original function, in the same order.
Note that you should not pass a function into the asyncAdapter
arguments, unless that function can return a value (e.g. not for an AJAX/Promise
-based function).
Here is an example of a non-asynchronous function being used with asyncAdapter
:
// Original functionconst add = ; // Add callback function to return valueconst asyncSum20 = ; // Add callback function to return value with side effectsconst asyncSum50 = ; // Use inside function to create DRY async version of original functionconst asyncSum = ; { const sum20 = await asyncSum20; const sum50 = await asyncSum50; const sum100 = await ; console; // -> 20 console; // -> 50 console; // -> 100}