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 without a callback and receive a promise! Object is not modified! Cool, ha?
I can promisify functions as well not only objecs 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.
npm i --save doasync
With objects:
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:
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 => {...});
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.
You can use it in asinc/await of course!
If you need a custom promisify function - use util.promisify.custom