租房小程序前端代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

120 lines
3.7 KiB

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