https://github.com/jiangjie/promisetoasync
Convert promise to async function.
https://github.com/jiangjie/promisetoasync
async promise
Last synced: 5 months ago
JSON representation
Convert promise to async function.
- Host: GitHub
- URL: https://github.com/jiangjie/promisetoasync
- Owner: JiangJie
- License: gpl-3.0
- Created: 2018-09-28T15:37:18.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2019-08-27T01:59:16.000Z (almost 6 years ago)
- Last Synced: 2024-04-24T04:29:09.648Z (about 1 year ago)
- Topics: async, promise
- Language: JavaScript
- Size: 30.3 KB
- Stars: 1
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# PromiseToAsync
[](https://www.npmjs.com/package/promise-to-async)
[](https://travis-ci.org/JiangJie/PromiseToAsync)
[](https://coveralls.io/github/JiangJie/PromiseToAsync?branch=master)
[](https://github.com/JiangJie/PromiseToAsync/blob/master/LICENSE)
[](https://david-dm.org/JiangJie/PromiseToAsync?type=dev)[](https://www.npmjs.com/package/promise-to-async)
[](https://nodei.co/npm/promise-to-async/)
Convert promise or promisify function to async function.
The new async function will return `[err, res]`, like nodejs callback style.
## Install
```javascript
npm install promise-to-async --save
```## Example
```javascript
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.
```javascript
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');
})();
```