Promisify objects immutably
Hey! I’m a tiny little npm module - doasync. 50 lines of code! Don’t ignore me, I’m very helpful! With me you can call any method of your object as if it was promisified! The object will not be modified. No extra variables and objects! Cool, ha?
doAsync(fs).readFile('package.json', 'utf8').then(result => {...})I can promisify functions as well not only objects and util.promisify guy helps me. But before we go, we must thank and give all credit to Proxy and ES6 guys… WeakMap has also proved useful.
Let’s go!
Like one kid said, util.promisify() (which is under the hood of this module) can interact directly with the V8 API, so it doesn't create closures and will be faster than userland implementations. Here is some more examples of the usage:
const fs = require('fs');
const doAsync = require('doasync');doAsync(fs).readFile('package.json', 'utf8').then((result) => {
console.dir(JSON.parse(result), {colors: true});
});
With functions
For example with request library:
doAsync(request)('http://www.google.com').then(({body}) => {
console.log(body);
/* ... */
});You can even use native call and apply to bind some context:
doAsync(myFunc).apply(context, params).then(result => {...});Isn’t it really promising?
Get Do Async’s stories in your inbox
Join Medium for free to get updates from this writer.









