Creating A Promise Wrapper for Old Callback API Methods

Franklyn Ugege
BitHubPH
3 min readApr 24, 2019

--

Adapting to asynchronous programming style can be a pain at times. Especially coming from a programming background like php.

Just recently, I logged off.

I made the silliest mistake in trying to consume an asynchronous API synchronously. I was actually returning the response of the callback and assigning the value to a variable in synchronous fashion.

Here’s what I mean.

const apiWrapper = (apiMethodCallArgument) => {
//apiMethodCall takes time to return a response,
//hence it asynchronous
apiMethodCall(apiMethodCallArgument, (err, responseObject) => {
if(err) {
//handle/report error
}
return responseObject;
});
}
//Assigning the response of a function that
//takes time to respond to a variable.
const apiResponse = apiWrapper(myApiArguments);

It was really dumb on second thought with the way Node.js works and the event loop, to think that apiResponse variable would contain the returned value from the apiWrapper function but rather returned “undefined”. The reason for this is because at the point of calling the function the response is not ready and so the default “undefined” response for function calls is returned and not the actual API response object.

An easy kill to create my own wrapper for the API was to use Promises. That was it. Within the promise resolve callback I could handle the responseObject in a clean way.

So here was is the simple solution to create Promise wrapper for callback styled API’s.

const promiseApiWrapper = (apiMethodCallArgument) => new Promise ((resolve, reject) => {
apiMethodCall(apiMethodCallArgument, (err, responseObject) =>
if(err) {
//handle/report error
return reject(err);
}
return resolve(responseObject);
});
});

So, let’s work through the solution above.

The promiseApiWrapper function basically returns a Promise. Within the promise callback is where the main API method is called which has an asynchronous callback style; in the callback the Promise resolve method is called with the responseObject if there’s no error from the API call else a reject function is called with the error object if need be.

Consuming the API wrapper comes with some ease and managing the response object too. Below is a simple sample of how I consumed the promisyfied API wrapper.

promiseApiWrapper(myApiArguments)
.then(responseObject => {
//manipulate the responseObject
})
.catch(e => {
//handle the error case
});

The nice thing about creating the Promisyfied API Wrapper function is that you can chain the Promised API wrappers neatly and only handle the error case in a single place making the code base neater and more readable.

I hope I’ve been able to share some useful insight with you on this problem of converting API methods in callback style to promisyfied API versions.

Life as a developer is one of continuous learning.

I recently came across a core Node.js function that promisifies callback functions out of the box. Here’s is a link to the resource and it is easy to follow (Util.promisify). Feel free to hit me up if you have any issues using it 😉.

--

--

Franklyn Ugege
BitHubPH

I am a fullstack developer with extensive experience in Javascript and Python and a passion for building highly configurable and user-friendly applications