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 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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-done
  7. [![NPM version][npm-image]][npm-url] [![Downloads][downloads-image]][npm-url] [![Azure Pipelines Build Status][azure-pipelines-image]][azure-pipelines-url] [![Travis 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. Allows libraries to handle various caller provided asynchronous functions uniformly. Maps promises, observables, child processes and streams, and callbacks to callback style.
  9. As async conventions evolve, it is useful to be able to deal with several different *styles* of async completion uniformly. With this module you can handle completion using a node-style callback, regardless of a return value that's a promise, observable, child process or stream.
  10. ## Usage
  11. ### Successful completion
  12. ```js
  13. var asyncDone = require('async-done');
  14. asyncDone(function(done){
  15. // do async things
  16. done(null, 2);
  17. }, function(error, result){
  18. // `error` will be null on successful execution of the first function.
  19. // `result` will be the result from the first function.
  20. });
  21. ```
  22. ### Failed completion
  23. ```js
  24. var asyncDone = require('async-done');
  25. asyncDone(function(done){
  26. // do async things
  27. done(new Error('Some Error Occurred'));
  28. }, function(error, result){
  29. // `error` will be an error from the first function.
  30. // `result` will be undefined on failed execution of the first function.
  31. });
  32. ```
  33. ## API
  34. ### `asyncDone(fn, callback)`
  35. Takes a function to execute (`fn`) and a function to call on completion (`callback`).
  36. #### `fn([done])`
  37. Optionally takes a callback to call when async tasks are complete.
  38. #### Completion and Error Resolution
  39. * `Callback` (`done`) called
  40. - Completion: called with null error
  41. - Error: called with non-null error
  42. * `Stream` or `EventEmitter` returned
  43. - Completion: [end-of-stream][end-of-stream] module
  44. - Error: [domains][domains]
  45. - __Note:__ Only actual streams are supported, not faux-streams; Therefore, modules like [`event-stream`][event-stream] are not supported.
  46. * `Child Process` returned
  47. - Completion [end-of-stream][end-of-stream] module
  48. - Error: [domains][domains]
  49. * `Promise` returned
  50. - Completion: [onFulfilled][promise-onfulfilled] method called
  51. - Error: [onRejected][promise-onrejected] method called
  52. * `Observable` (e.g. from [RxJS v5][rxjs5-observable] or [RxJS v4][rxjs5-observable]) returned
  53. - Completion: [complete][rxjs5-observer-complete] method called
  54. - Error: [error][rxjs5-observer-error] method called
  55. __Warning:__ Sync tasks are __not supported__ and your function will never complete if the one of the above strategies is not used to signal completion. However, thrown errors will be caught by the domain.
  56. #### `callback(error, result)`
  57. If an error doesn't occur in the execution of the `fn` function, the `callback` method will receive the results as its second argument. Note: Some streams don't received any results.
  58. If an error occurred in the execution of the `fn` function, The `callback` method will receive an error as its first argument.
  59. Errors can be caused by:
  60. * A thrown error
  61. * An error passed to a `done` callback
  62. * An `error` event emitted on a returned `Stream`, `EventEmitter` or `Child Process`
  63. * A rejection of a returned `Promise` - If the `Promise` is not rejected with a value, we generate a new `Error`
  64. * The `onError` handler being called on an `Observable`
  65. ## License
  66. MIT
  67. [downloads-image]: https://img.shields.io/npm/dm/async-done.svg
  68. [npm-url]: https://www.npmjs.com/package/async-done
  69. [npm-image]: https://img.shields.io/npm/v/async-done.svg
  70. [azure-pipelines-url]: https://dev.azure.com/gulpjs/gulp/_build/latest?definitionId=6&branchName=master
  71. [azure-pipelines-image]: https://dev.azure.com/gulpjs/gulp/_apis/build/status/async-done?branchName=master
  72. [travis-url]: https://travis-ci.org/gulpjs/async-done
  73. [travis-image]: https://img.shields.io/travis/gulpjs/async-done.svg?label=travis-ci
  74. [appveyor-url]: https://ci.appveyor.com/project/gulpjs/async-done
  75. [appveyor-image]: https://img.shields.io/appveyor/ci/gulpjs/async-done.svg?label=appveyor
  76. [coveralls-url]: https://coveralls.io/r/gulpjs/async-done
  77. [coveralls-image]: https://img.shields.io/coveralls/gulpjs/async-done/master.svg
  78. [gitter-url]: https://gitter.im/gulpjs/gulp
  79. [gitter-image]: https://badges.gitter.im/gulpjs/gulp.svg
  80. [end-of-stream]: https://www.npmjs.com/package/end-of-stream
  81. [domains]: http://nodejs.org/api/domain.html
  82. [event-stream]: https://github.com/dominictarr/event-stream
  83. [promise-onfulfilled]: http://promisesaplus.com/#point-26
  84. [promise-onrejected]: http://promisesaplus.com/#point-30
  85. [rx4-observable]: https://github.com/Reactive-Extensions/RxJS/blob/master/doc/api/core/observable.md
  86. [rxjs5-observable]: http://reactivex.io/rxjs/class/es6/Observable.js~Observable.html
  87. [rxjs5-observer-complete]: http://reactivex.io/rxjs/class/es6/MiscJSDoc.js~ObserverDoc.html#instance-method-complete
  88. [rxjs5-observer-error]: http://reactivex.io/rxjs/class/es6/MiscJSDoc.js~ObserverDoc.html#instance-method-error