Skip to content

doasync/doasync

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

23 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

DoAsync logo

NPM Version NPM Downloads GitHub issues Telegram Tweet

Welcome to the async world!

Hey! I’m a tiny little npm module - doasync. ~40 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 objects) with the help of util.promisify. But first of all, we must give credit to Proxy and ES6 guys... WeakMap has also proved useful.


Installation

npm i --save doasync

Usage

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.

Memoization is used in the module to prevent functions from being promisified each time (with the use of WeakMaps).

You can use it in async/await of course!

Tip


If you need a custom promisify function - use util.promisify.custom