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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. # now-and-later
  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. Map over an array or object of values in parallel or series, passing each through the async iterator, with optional lifecycle hooks.
  9. ## Usage
  10. ```js
  11. var nal = require('now-and-later');
  12. function iterator(value, key, cb){
  13. // called with each value in sequence
  14. // also passes the key
  15. cb(null, value * 2)
  16. }
  17. function create(value, key){
  18. // called at the beginning of every iteration
  19. // return a storage object to be passed to each lifecycle method
  20. return { key: key, value: value };
  21. }
  22. function before(storage){
  23. // called before the iterator function of every iteration
  24. // receives the storage returned from `create`
  25. }
  26. function after(result, storage){
  27. // called after a success occurs in the iterator function of any iteration
  28. // receives the `result` of the iterator and the storage returned from `create`
  29. }
  30. function error(error, storage){
  31. // called after an error occurs in the iterator function of any iteration
  32. // receives the `error` of the iterator and the storage returned from `create`
  33. }
  34. function done(error, results) {
  35. // called after all iterations complete or an error occurs in an iterator
  36. // receives an `error` if one occurred and all results (or partial results upon error) of the iterators
  37. }
  38. /*
  39. Calling mapSeries with an object can't guarantee order
  40. It uses Object.keys to get an order
  41. It is better to use an array if order must be guaranteed
  42. */
  43. nal.mapSeries([1, 2, 3], iterator, {
  44. create: create,
  45. before: before,
  46. after: after,
  47. error: error
  48. }, done);
  49. nal.map({
  50. iter1: 1,
  51. iter2: 2
  52. }, iterator, {
  53. create: create,
  54. before: before,
  55. after: after,
  56. error: error
  57. }, done);
  58. ```
  59. ## API
  60. ### `map(values, iterator[, extensions][, callback])`
  61. Takes an object or array of `values` and an `iterator` function to execute with each value.
  62. Optionally, takes an `extensions` object and a `callback` function that is called upon completion of the iterations.
  63. All iterations run in parallel.
  64. #### `values`
  65. An array or object of values to iterate over.
  66. If `values` is an array, iterations are started in order by index. If `values` is an object, iterations are started in order by the order returned by `Object.keys` (order is not guaranteed).
  67. If `values` is an array, the results of each iteration will be mapped to an array. If `values` is an object, the results of each iteration will be mapped to an object with corresponding keys.
  68. #### `iterator(value, key, done)`
  69. An async function called per iteration. All iterations are run in parallel.
  70. The `iterator` function is called once with each `value`, `key` and a function (`done(error, result)`) to call when the async work is complete.
  71. If `done` is passed an error as the first argument, the iteration will fail and the sequence will be ended; however, any iterations in progress will still complete. If `done` is passed a `result` value as the second argument, it will be added to the final results array or object.
  72. #### `extensions`
  73. The `extensions` object is used for specifying functions that give insight into the lifecycle of each iteration. The possible extension points are `create`, `before`, `after` and `error`. If an extension point is not specified, it defaults to a no-op function.
  74. ##### `extensions.create(value, key)`
  75. Called at the very beginning of each iteration with the `value` being iterated and the `key` from the array or object. If `create` returns a value (`storage`), it is passed to the `before`, `after` and `error` extension points.
  76. If a value is not returned, an empty object is used as `storage` for each other extension point.
  77. This is useful for tracking information across an iteration.
  78. ##### `extensions.before(storage)`
  79. Called immediately before each iteration with the `storage` value returned from the `create` extension point.
  80. ##### `extensions.after(result, storage)`
  81. Called immediately after each iteration with the `result` of the iteration and the `storage` value returned from the `create` extension point.
  82. ##### `extensions.error(error, storage)`
  83. Called immediately after a failed iteration with the `error` of the iteration and the `storage` value returned from the `create` extension point.
  84. #### `callback(error, results)`
  85. A function that is called after all iterations have completed or one iteration has errored.
  86. If all iterations completed successfully, the `error` argument will be empty and the `results` will be a mapping of the `iterator` results.
  87. If an iteration errored, the `error` argument will be passed from that iteration and the `results` will be whatever partial results had completed successfully before the error occurred.
  88. ### `mapSeries(values, iterator[, extensions][, callback])`
  89. Takes an object or array of `values` and an `iterator` function to execute with each value.
  90. Optionally, takes an `extensions` object and a `callback` function that is called upon completion of the iterations.
  91. All iterations run in serial.
  92. #### `values`
  93. An array or object of values to iterate over.
  94. If `values` is an array, iterations are started in order by index. If `values` is an object, iterations are started in order by the order returned by `Object.keys` (order is not guaranteed).
  95. If `values` is an array, the results of each iteration will be mapped to an array. If `values` is an object, the results of each iteration will be mapped to an object with corresponding keys.
  96. #### `iterator(value, key, done)`
  97. An async function called per iteration. All iterations are run in serial.
  98. The `iterator` function is called once with each `value`, `key` and a function (`done(error, result)`) to call when the async work is complete.
  99. If `done` is passed an error as the first argument, the iteration will fail and the sequence will be ended without executing any more iterations. If `done` is passed a `result` value as the second argument, it will be added to the final results array or object.
  100. #### `extensions`
  101. The `extensions` object is used for specifying functions that give insight into the lifecycle of each iteration. The possible extension points are `create`, `before`, `after` and `error`. If an extension point is not specified, it defaults to a no-op function.
  102. ##### `extensions.create(value, key)`
  103. Called at the very beginning of each iteration with the `value` being iterated and the `key` from the array or object. If `create` returns a value (`storage`), it is passed to the `before`, `after` and `error` extension points.
  104. If a value is not returned, an empty object is used as `storage` for each other extension point.
  105. This is useful for tracking information across an iteration.
  106. ##### `extensions.before(storage)`
  107. Called immediately before each iteration with the `storage` value returned from the `create` extension point.
  108. ##### `extensions.after(result, storage)`
  109. Called immediately after each iteration with the `result` of the iteration and the `storage` value returned from the `create` extension point.
  110. ##### `extensions.error(error, storage)`
  111. Called immediately after a failed iteration with the `error` of the iteration and the `storage` value returned from the `create` extension point.
  112. #### `callback(error, results)`
  113. A function that is called after all iterations have completed or one iteration has errored.
  114. If all iterations completed successfully, the `error` argument will be empty and the `results` will be a mapping of the `iterator` results.
  115. If an iteration errored, the `error` argument will be passed from that iteration and the `results` will be whatever partial results had completed successfully before the error occurred.
  116. ## License
  117. MIT
  118. [downloads-image]: http://img.shields.io/npm/dm/now-and-later.svg
  119. [npm-url]: https://www.npmjs.com/package/now-and-later
  120. [npm-image]: http://img.shields.io/npm/v/now-and-later.svg
  121. [travis-url]: https://travis-ci.org/gulpjs/now-and-later
  122. [travis-image]: http://img.shields.io/travis/gulpjs/now-and-later.svg?label=travis-ci
  123. [appveyor-url]: https://ci.appveyor.com/project/gulpjs/now-and-later
  124. [appveyor-image]: https://img.shields.io/appveyor/ci/gulpjs/now-and-later.svg?label=appveyor
  125. [coveralls-url]: https://coveralls.io/r/gulpjs/now-and-later
  126. [coveralls-image]: http://img.shields.io/coveralls/gulpjs/now-and-later/master.svg
  127. [gitter-url]: https://gitter.im/gulpjs/gulp
  128. [gitter-image]: https://badges.gitter.im/gulpjs/gulp.svg