| 1 | 'use strict';
|
|---|
| 2 |
|
|---|
| 3 | Object.defineProperty(exports, "__esModule", {
|
|---|
| 4 | value: true
|
|---|
| 5 | });
|
|---|
| 6 |
|
|---|
| 7 | var _concatLimit = require('./concatLimit.js');
|
|---|
| 8 |
|
|---|
| 9 | var _concatLimit2 = _interopRequireDefault(_concatLimit);
|
|---|
| 10 |
|
|---|
| 11 | var _awaitify = require('./internal/awaitify.js');
|
|---|
| 12 |
|
|---|
| 13 | var _awaitify2 = _interopRequireDefault(_awaitify);
|
|---|
| 14 |
|
|---|
| 15 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|---|
| 16 |
|
|---|
| 17 | /**
|
|---|
| 18 | * Applies `iteratee` to each item in `coll`, concatenating the results. Returns
|
|---|
| 19 | * the concatenated list. The `iteratee`s are called in parallel, and the
|
|---|
| 20 | * results are concatenated as they return. The results array will be returned in
|
|---|
| 21 | * the original order of `coll` passed to the `iteratee` function.
|
|---|
| 22 | *
|
|---|
| 23 | * @name concat
|
|---|
| 24 | * @static
|
|---|
| 25 | * @memberOf module:Collections
|
|---|
| 26 | * @method
|
|---|
| 27 | * @category Collection
|
|---|
| 28 | * @alias flatMap
|
|---|
| 29 | * @param {Array|Iterable|AsyncIterable|Object} coll - A collection to iterate over.
|
|---|
| 30 | * @param {AsyncFunction} iteratee - A function to apply to each item in `coll`,
|
|---|
| 31 | * which should use an array as its result. Invoked with (item, callback).
|
|---|
| 32 | * @param {Function} [callback] - A callback which is called after all the
|
|---|
| 33 | * `iteratee` functions have finished, or an error occurs. Results is an array
|
|---|
| 34 | * containing the concatenated results of the `iteratee` function. Invoked with
|
|---|
| 35 | * (err, results).
|
|---|
| 36 | * @returns A Promise, if no callback is passed
|
|---|
| 37 | * @example
|
|---|
| 38 | *
|
|---|
| 39 | * // dir1 is a directory that contains file1.txt, file2.txt
|
|---|
| 40 | * // dir2 is a directory that contains file3.txt, file4.txt
|
|---|
| 41 | * // dir3 is a directory that contains file5.txt
|
|---|
| 42 | * // dir4 does not exist
|
|---|
| 43 | *
|
|---|
| 44 | * let directoryList = ['dir1','dir2','dir3'];
|
|---|
| 45 | * let withMissingDirectoryList = ['dir1','dir2','dir3', 'dir4'];
|
|---|
| 46 | *
|
|---|
| 47 | * // Using callbacks
|
|---|
| 48 | * async.concat(directoryList, fs.readdir, function(err, results) {
|
|---|
| 49 | * if (err) {
|
|---|
| 50 | * console.log(err);
|
|---|
| 51 | * } else {
|
|---|
| 52 | * console.log(results);
|
|---|
| 53 | * // [ 'file1.txt', 'file2.txt', 'file3.txt', 'file4.txt', file5.txt ]
|
|---|
| 54 | * }
|
|---|
| 55 | * });
|
|---|
| 56 | *
|
|---|
| 57 | * // Error Handling
|
|---|
| 58 | * async.concat(withMissingDirectoryList, fs.readdir, function(err, results) {
|
|---|
| 59 | * if (err) {
|
|---|
| 60 | * console.log(err);
|
|---|
| 61 | * // [ Error: ENOENT: no such file or directory ]
|
|---|
| 62 | * // since dir4 does not exist
|
|---|
| 63 | * } else {
|
|---|
| 64 | * console.log(results);
|
|---|
| 65 | * }
|
|---|
| 66 | * });
|
|---|
| 67 | *
|
|---|
| 68 | * // Using Promises
|
|---|
| 69 | * async.concat(directoryList, fs.readdir)
|
|---|
| 70 | * .then(results => {
|
|---|
| 71 | * console.log(results);
|
|---|
| 72 | * // [ 'file1.txt', 'file2.txt', 'file3.txt', 'file4.txt', file5.txt ]
|
|---|
| 73 | * }).catch(err => {
|
|---|
| 74 | * console.log(err);
|
|---|
| 75 | * });
|
|---|
| 76 | *
|
|---|
| 77 | * // Error Handling
|
|---|
| 78 | * async.concat(withMissingDirectoryList, fs.readdir)
|
|---|
| 79 | * .then(results => {
|
|---|
| 80 | * console.log(results);
|
|---|
| 81 | * }).catch(err => {
|
|---|
| 82 | * console.log(err);
|
|---|
| 83 | * // [ Error: ENOENT: no such file or directory ]
|
|---|
| 84 | * // since dir4 does not exist
|
|---|
| 85 | * });
|
|---|
| 86 | *
|
|---|
| 87 | * // Using async/await
|
|---|
| 88 | * async () => {
|
|---|
| 89 | * try {
|
|---|
| 90 | * let results = await async.concat(directoryList, fs.readdir);
|
|---|
| 91 | * console.log(results);
|
|---|
| 92 | * // [ 'file1.txt', 'file2.txt', 'file3.txt', 'file4.txt', file5.txt ]
|
|---|
| 93 | * } catch (err) {
|
|---|
| 94 | * console.log(err);
|
|---|
| 95 | * }
|
|---|
| 96 | * }
|
|---|
| 97 | *
|
|---|
| 98 | * // Error Handling
|
|---|
| 99 | * async () => {
|
|---|
| 100 | * try {
|
|---|
| 101 | * let results = await async.concat(withMissingDirectoryList, fs.readdir);
|
|---|
| 102 | * console.log(results);
|
|---|
| 103 | * } catch (err) {
|
|---|
| 104 | * console.log(err);
|
|---|
| 105 | * // [ Error: ENOENT: no such file or directory ]
|
|---|
| 106 | * // since dir4 does not exist
|
|---|
| 107 | * }
|
|---|
| 108 | * }
|
|---|
| 109 | *
|
|---|
| 110 | */
|
|---|
| 111 | function concat(coll, iteratee, callback) {
|
|---|
| 112 | return (0, _concatLimit2.default)(coll, Infinity, iteratee, callback);
|
|---|
| 113 | }
|
|---|
| 114 | exports.default = (0, _awaitify2.default)(concat, 3);
|
|---|
| 115 | module.exports = exports.default; |
|---|