promise-me-async
v1.0.0
Published
Utility to convert callback functions into modern promises
Downloads
2
Readme
promise-me
Things in JavaScript have evolutionated a lot in the last years. The backwards compability from JavaScript is probably the best around all the languages, however something good typically came with something not that good.
One of the problems of old JavaScript are callbacks, and specially callback hells. Modern JavaScript looks like this:
async myClassMethod(){
await this.someWork();
await this.someOtherWork();
await this.someOtherWork2();
//congratulations, job completed!
}
Former JavaScript would be:
myClassMethod(callback){
someWork(function(){
someOtherWork(function(){
someOtherWork2(function(){
//Your ugly job is completed!
callback();
});
});
});
}
Having the context that modern JavaScript looks nice and because of backwards compabilities we see tons of code with callbacks, what if we make the callbacks nicer, that is why I created promise-me.
Usage, converting one of the most common callback functions, setTimeout:
//reply is a placeholder to capture arguments from your callback (if any)
let reply = {};
await PromiseMe.please(setTimeout, reply, 3000);
Comparing code waiting 3000 miliseconds:
//before waiting
await PromiseMe.please(setTimeout, {}, 3000);
//after waiting, I'm happy no more callback hells
//before waiting
setTimeout(() => {
//after waiting, I'm the first step of a callback hell
},3000);
Usage on a custom callback function()
function myCallback(name, callback){
callback(`The best magician: ` + name);
}
Typical usage:
myCallback((newName) => {
//Output: The best magician: Celi
console.log(newName);
}, 'Celi');
Promised way:
let reply = {};
await PromiseMe.please(myCallback, reply, `celi`);
//Output: The best magician: Celi
console.log(reply.arguments[0]);
Running tests:
npm test
Output from tests: