promise-to-async
v0.1.3
Published
Convert promise to async function.
Downloads
0
Maintainers
Readme
PromiseToAsync
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');
})();