Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/jiangjie/promisetoasync
Convert promise to async function.
https://github.com/jiangjie/promisetoasync
async promise
Last synced: about 2 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 (over 6 years ago)
- Default Branch: master
- Last Pushed: 2019-08-27T01:59:16.000Z (over 5 years ago)
- Last Synced: 2024-04-24T04:29:09.648Z (8 months 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
[![NPM version](https://img.shields.io/npm/v/promise-to-async.svg?style=flat-square)](https://www.npmjs.com/package/promise-to-async)
[![Build Status](https://travis-ci.org/JiangJie/PromiseToAsync.svg?branch=master)](https://travis-ci.org/JiangJie/PromiseToAsync)
[![Coverage Status](https://coveralls.io/repos/github/JiangJie/PromiseToAsync/badge.svg?branch=master)](https://coveralls.io/github/JiangJie/PromiseToAsync?branch=master)
[![GitHub license](https://img.shields.io/github/license/JiangJie/PromiseToAsync.svg)](https://github.com/JiangJie/PromiseToAsync/blob/master/LICENSE)
[![devDependencies Status](https://david-dm.org/JiangJie/PromiseToAsync/dev-status.svg)](https://david-dm.org/JiangJie/PromiseToAsync?type=dev)[![npm](https://img.shields.io/npm/dt/promise-to-async.svg)](https://www.npmjs.com/package/promise-to-async)
[![NPM](https://nodei.co/npm/promise-to-async.png?downloads=true)](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');
})();
```