promise-to-async
TypeScript icon, indicating that this package has built-in type declarations

0.1.3 • Public • Published

PromiseToAsync

NPM version Build Status Coverage Status GitHub license devDependencies Status

npm

NPM

Convert promise or promisify function to async function.

The new async function will return [err, res], like nodejs callback style.

Install

npm install promise-to-async --save

Example

import PromiseToAsync from 'promise-to-async';
 
(async () => {
    const promise = Promise.resolve('promise resolved');
 
    const [err, res] = await PromiseToAsync(promise);
 
    console.assert(err === null);
    console.assert(res === 'promise resolved');
})();
 
(async () => {
    const util = require('util');
    const fs = require('fs');
 
    const promise = util.promisify(fs.readdir)('/');
 
    const [err, res] = await PromiseToAsync(promise);
})();
// same as
(async () => {
    const util = require('util');
    const fs = require('fs');
 
    const readdir = PromiseToAsync(util.promisify(fs.readdir));
 
    const [err, res] = await readdir('/');
})();

Or by inject.

import PromiseToAsync from 'promise-to-async';
// inject
PromiseToAsync.injectAsyncMethod('two'); // method name default is two
 
(async () => {
    const promise = Promise.resolve('promise resolved');
 
    const [err, res] = await promise.two();
 
    console.assert(err === null);
    console.assert(res === 'promise resolved');
})();

Package Sidebar

Install

npm i promise-to-async

Weekly Downloads

2

Version

0.1.3

License

GPL-3.0

Unpacked Size

42.9 kB

Total Files

9

Last publish

Collaborators

  • jarvisjiang