| 1 |
|
|---|
| 2 | # thenify
|
|---|
| 3 |
|
|---|
| 4 | [![NPM version][npm-image]][npm-url]
|
|---|
| 5 | [![Build status][travis-image]][travis-url]
|
|---|
| 6 | [![Test coverage][coveralls-image]][coveralls-url]
|
|---|
| 7 | [![Dependency Status][david-image]][david-url]
|
|---|
| 8 | [![License][license-image]][license-url]
|
|---|
| 9 | [![Downloads][downloads-image]][downloads-url]
|
|---|
| 10 |
|
|---|
| 11 | Promisify a callback-based function using [`any-promise`](https://github.com/kevinbeaty/any-promise).
|
|---|
| 12 |
|
|---|
| 13 | - Preserves function names
|
|---|
| 14 | - Uses a native promise implementation if available and tries to fall back to a promise implementation such as `bluebird`
|
|---|
| 15 | - Converts multiple arguments from the callback into an `Array`, also support change the behavior by `options.multiArgs`
|
|---|
| 16 | - Resulting function never deoptimizes
|
|---|
| 17 | - Supports both callback and promise style
|
|---|
| 18 |
|
|---|
| 19 | An added benefit is that `throw`n errors in that async function will be caught by the promise!
|
|---|
| 20 |
|
|---|
| 21 | ## API
|
|---|
| 22 |
|
|---|
| 23 | ### fn = thenify(fn, options)
|
|---|
| 24 |
|
|---|
| 25 | Promisifies a function.
|
|---|
| 26 |
|
|---|
| 27 | ### Options
|
|---|
| 28 |
|
|---|
| 29 | `options` are optional.
|
|---|
| 30 |
|
|---|
| 31 | - `options.withCallback` - support both callback and promise style, default to `false`.
|
|---|
| 32 | - `options.multiArgs` - change the behavior when callback have multiple arguments. default to `true`.
|
|---|
| 33 | - `true` - converts multiple arguments to an array
|
|---|
| 34 | - `false`- always use the first argument
|
|---|
| 35 | - `Array` - converts multiple arguments to an object with keys provided in `options.multiArgs`
|
|---|
| 36 |
|
|---|
| 37 | - Turn async functions into promises
|
|---|
| 38 |
|
|---|
| 39 | ```js
|
|---|
| 40 | var thenify = require('thenify');
|
|---|
| 41 |
|
|---|
| 42 | var somethingAsync = thenify(function somethingAsync(a, b, c, callback) {
|
|---|
| 43 | callback(null, a, b, c);
|
|---|
| 44 | });
|
|---|
| 45 | ```
|
|---|
| 46 |
|
|---|
| 47 | - Backward compatible with callback
|
|---|
| 48 |
|
|---|
| 49 | ```js
|
|---|
| 50 | var thenify = require('thenify');
|
|---|
| 51 |
|
|---|
| 52 | var somethingAsync = thenify(function somethingAsync(a, b, c, callback) {
|
|---|
| 53 | callback(null, a, b, c);
|
|---|
| 54 | }, { withCallback: true });
|
|---|
| 55 |
|
|---|
| 56 | // somethingAsync(a, b, c).then(onFulfilled).catch(onRejected);
|
|---|
| 57 | // somethingAsync(a, b, c, function () {});
|
|---|
| 58 | ```
|
|---|
| 59 |
|
|---|
| 60 | or use `thenify.withCallback()`
|
|---|
| 61 |
|
|---|
| 62 | ```js
|
|---|
| 63 | var thenify = require('thenify').withCallback;
|
|---|
| 64 |
|
|---|
| 65 | var somethingAsync = thenify(function somethingAsync(a, b, c, callback) {
|
|---|
| 66 | callback(null, a, b, c);
|
|---|
| 67 | });
|
|---|
| 68 |
|
|---|
| 69 | // somethingAsync(a, b, c).then(onFulfilled).catch(onRejected);
|
|---|
| 70 | // somethingAsync(a, b, c, function () {});
|
|---|
| 71 | ```
|
|---|
| 72 |
|
|---|
| 73 | - Always return the first argument in callback
|
|---|
| 74 |
|
|---|
| 75 | ```js
|
|---|
| 76 | var thenify = require('thenify');
|
|---|
| 77 |
|
|---|
| 78 | var promise = thenify(function (callback) {
|
|---|
| 79 | callback(null, 1, 2, 3);
|
|---|
| 80 | }, { multiArgs: false });
|
|---|
| 81 |
|
|---|
| 82 | // promise().then(function onFulfilled(value) {
|
|---|
| 83 | // assert.equal(value, 1);
|
|---|
| 84 | // });
|
|---|
| 85 | ```
|
|---|
| 86 |
|
|---|
| 87 | - Converts callback arguments to an object
|
|---|
| 88 |
|
|---|
| 89 | ```js
|
|---|
| 90 | var thenify = require('thenify');
|
|---|
| 91 |
|
|---|
| 92 | var promise = thenify(function (callback) {
|
|---|
| 93 | callback(null, 1, 2, 3);
|
|---|
| 94 | }, { multiArgs: [ 'one', 'tow', 'three' ] });
|
|---|
| 95 |
|
|---|
| 96 | // promise().then(function onFulfilled(value) {
|
|---|
| 97 | // assert.deepEqual(value, {
|
|---|
| 98 | // one: 1,
|
|---|
| 99 | // tow: 2,
|
|---|
| 100 | // three: 3
|
|---|
| 101 | // });
|
|---|
| 102 | // });
|
|---|
| 103 | ```
|
|---|
| 104 |
|
|---|
| 105 | [gitter-image]: https://badges.gitter.im/thenables/thenify.png
|
|---|
| 106 | [gitter-url]: https://gitter.im/thenables/thenify
|
|---|
| 107 | [npm-image]: https://img.shields.io/npm/v/thenify.svg?style=flat-square
|
|---|
| 108 | [npm-url]: https://npmjs.org/package/thenify
|
|---|
| 109 | [github-tag]: http://img.shields.io/github/tag/thenables/thenify.svg?style=flat-square
|
|---|
| 110 | [github-url]: https://github.com/thenables/thenify/tags
|
|---|
| 111 | [travis-image]: https://img.shields.io/travis/thenables/thenify.svg?style=flat-square
|
|---|
| 112 | [travis-url]: https://travis-ci.org/thenables/thenify
|
|---|
| 113 | [coveralls-image]: https://img.shields.io/coveralls/thenables/thenify.svg?style=flat-square
|
|---|
| 114 | [coveralls-url]: https://coveralls.io/r/thenables/thenify
|
|---|
| 115 | [david-image]: http://img.shields.io/david/thenables/thenify.svg?style=flat-square
|
|---|
| 116 | [david-url]: https://david-dm.org/thenables/thenify
|
|---|
| 117 | [license-image]: http://img.shields.io/npm/l/thenify.svg?style=flat-square
|
|---|
| 118 | [license-url]: LICENSE
|
|---|
| 119 | [downloads-image]: http://img.shields.io/npm/dm/thenify.svg?style=flat-square
|
|---|
| 120 | [downloads-url]: https://npmjs.org/package/thenify
|
|---|