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.

README.md 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <p align="center">
  2. <a href="http://gulpjs.com">
  3. <img height="257" width="114" src="https://raw.githubusercontent.com/gulpjs/artwork/master/gulp-2x.png">
  4. </a>
  5. </p>
  6. # async-settle
  7. [![NPM version][npm-image]][npm-url] [![Downloads][downloads-image]][npm-url] [![Build Status][travis-image]][travis-url] [![AppVeyor Build Status][appveyor-image]][appveyor-url] [![Coveralls Status][coveralls-image]][coveralls-url] [![Gitter chat][gitter-image]][gitter-url]
  8. Settle an async function. It will always complete successfully with an object of the resulting state.
  9. Handles completion and errors for callbacks, promises, observables and streams.
  10. Will run call the function on `nextTick`. This will cause all functions to be async.
  11. ## Usage
  12. ### Successful completion
  13. ```js
  14. var asyncSettle = require('async-settle');
  15. asyncSettle(function(done){
  16. // do async things
  17. done(null, 2);
  18. }, function(error, result){
  19. // `error` will ALWAYS be null on execution of the first function.
  20. // `result` will ALWAYS be a settled object with the result or error of the first function.
  21. });
  22. ```
  23. ### Failed completion
  24. ```js
  25. var asyncSettle = require('async-settle');
  26. asyncSettle(function(done){
  27. // do async things
  28. done(new Error('Some Error Occurred'));
  29. }, function(error, result){
  30. // `error` will ALWAYS be null on execution of the first function.
  31. // `result` will ALWAYS be a settled object with the result or error of the first function.
  32. });
  33. ```
  34. ## API
  35. ### `asyncSettle(fn, callback)`
  36. Takes a function to execute (`fn`) and a function to call on completion (`callback`).
  37. #### `fn([done])`
  38. Optionally takes a callback (`done`) to call when async tasks are complete.
  39. Executed in the context of [`async-done`][async-done], with all errors and results being settled.
  40. Completion is handled by [`async-done` completion and error resolution][completions].
  41. #### `callback(error, result)`
  42. Called on completion of `fn` and recieves a settled object as the `result` argument.
  43. The `error` argument will always be `null`.
  44. #### Settled Object
  45. Settled values have two properties, `state` and `value`.
  46. `state` has two possible options `'error'` and `'success'`.
  47. `value` will be the value passed to original callback.
  48. ## License
  49. MIT
  50. [async-done]: https://github.com/gulpjs/async-done
  51. [completions]: https://github.com/gulpjs/async-done#completion-and-error-resolution
  52. [downloads-image]: http://img.shields.io/npm/dm/async-settle.svg
  53. [npm-url]: https://www.npmjs.com/package/async-settle
  54. [npm-image]: http://img.shields.io/npm/v/async-settle.svg
  55. [travis-url]: https://travis-ci.org/gulpjs/async-settle
  56. [travis-image]: http://img.shields.io/travis/gulpjs/async-settle.svg?label=travis-ci
  57. [appveyor-url]: https://ci.appveyor.com/project/gulpjs/async-settle
  58. [appveyor-image]: https://img.shields.io/appveyor/ci/gulpjs/async-settle.svg?label=appveyor
  59. [coveralls-url]: https://coveralls.io/r/gulpjs/async-settle
  60. [coveralls-image]: http://img.shields.io/coveralls/gulpjs/async-settle/master.svg
  61. [gitter-url]: https://gitter.im/gulpjs/gulp
  62. [gitter-image]: https://badges.gitter.im/gulpjs/gulp.svg