Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including doasync with all npm packages installed. Try it out:

var doasync = require("doasync")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

doasync v2.0.1

Promisify everything without modification using util.promisify and ES6 Proxy

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

Metadata

RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free