CoolFace
Datasetpublic

basant307/AI_Governance_Project

sourceHugging Faceapache-2.0updated 2mo agoView on Hugging Face
0likes45downloads
inject.js153 linesDownload Raw Back to async
1'use strict';2 3Object.defineProperty(exports, "__esModule", {4    value: true5});6 7var _eachOfSeries = require('./eachOfSeries.js');8 9var _eachOfSeries2 = _interopRequireDefault(_eachOfSeries);10 11var _once = require('./internal/once.js');12 13var _once2 = _interopRequireDefault(_once);14 15var _wrapAsync = require('./internal/wrapAsync.js');16 17var _wrapAsync2 = _interopRequireDefault(_wrapAsync);18 19var _awaitify = require('./internal/awaitify.js');20 21var _awaitify2 = _interopRequireDefault(_awaitify);22 23function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }24 25/**26 * Reduces `coll` into a single value using an async `iteratee` to return each27 * successive step. `memo` is the initial state of the reduction. This function28 * only operates in series.29 *30 * For performance reasons, it may make sense to split a call to this function31 * into a parallel map, and then use the normal `Array.prototype.reduce` on the32 * results. This function is for situations where each step in the reduction33 * needs to be async; if you can get the data before reducing it, then it's34 * probably a good idea to do so.35 *36 * @name reduce37 * @static38 * @memberOf module:Collections39 * @method40 * @alias inject41 * @alias foldl42 * @category Collection43 * @param {Array|Iterable|AsyncIterable|Object} coll - A collection to iterate over.44 * @param {*} memo - The initial state of the reduction.45 * @param {AsyncFunction} iteratee - A function applied to each item in the46 * array to produce the next step in the reduction.47 * The `iteratee` should complete with the next state of the reduction.48 * If the iteratee completes with an error, the reduction is stopped and the49 * main `callback` is immediately called with the error.50 * Invoked with (memo, item, callback).51 * @param {Function} [callback] - A callback which is called after all the52 * `iteratee` functions have finished. Result is the reduced value. Invoked with53 * (err, result).54 * @returns {Promise} a promise, if no callback is passed55 * @example56 *57 * // file1.txt is a file that is 1000 bytes in size58 * // file2.txt is a file that is 2000 bytes in size59 * // file3.txt is a file that is 3000 bytes in size60 * // file4.txt does not exist61 *62 * const fileList = ['file1.txt','file2.txt','file3.txt'];63 * const withMissingFileList = ['file1.txt','file2.txt','file3.txt', 'file4.txt'];64 *65 * // asynchronous function that computes the file size in bytes66 * // file size is added to the memoized value, then returned67 * function getFileSizeInBytes(memo, file, callback) {68 *     fs.stat(file, function(err, stat) {69 *         if (err) {70 *             return callback(err);71 *         }72 *         callback(null, memo + stat.size);73 *     });74 * }75 *76 * // Using callbacks77 * async.reduce(fileList, 0, getFileSizeInBytes, function(err, result) {78 *     if (err) {79 *         console.log(err);80 *     } else {81 *         console.log(result);82 *         // 600083 *         // which is the sum of the file sizes of the three files84 *     }85 * });86 *87 * // Error Handling88 * async.reduce(withMissingFileList, 0, getFileSizeInBytes, function(err, result) {89 *     if (err) {90 *         console.log(err);91 *         // [ Error: ENOENT: no such file or directory ]92 *     } else {93 *         console.log(result);94 *     }95 * });96 *97 * // Using Promises98 * async.reduce(fileList, 0, getFileSizeInBytes)99 * .then( result => {100 *     console.log(result);101 *     // 6000102 *     // which is the sum of the file sizes of the three files103 * }).catch( err => {104 *     console.log(err);105 * });106 *107 * // Error Handling108 * async.reduce(withMissingFileList, 0, getFileSizeInBytes)109 * .then( result => {110 *     console.log(result);111 * }).catch( err => {112 *     console.log(err);113 *     // [ Error: ENOENT: no such file or directory ]114 * });115 *116 * // Using async/await117 * async () => {118 *     try {119 *         let result = await async.reduce(fileList, 0, getFileSizeInBytes);120 *         console.log(result);121 *         // 6000122 *         // which is the sum of the file sizes of the three files123 *     }124 *     catch (err) {125 *         console.log(err);126 *     }127 * }128 *129 * // Error Handling130 * async () => {131 *     try {132 *         let result = await async.reduce(withMissingFileList, 0, getFileSizeInBytes);133 *         console.log(result);134 *     }135 *     catch (err) {136 *         console.log(err);137 *         // [ Error: ENOENT: no such file or directory ]138 *     }139 * }140 *141 */142function reduce(coll, memo, iteratee, callback) {143    callback = (0, _once2.default)(callback);144    var _iteratee = (0, _wrapAsync2.default)(iteratee);145    return (0, _eachOfSeries2.default)(coll, (x, i, iterCb) => {146        _iteratee(memo, x, (err, v) => {147            memo = v;148            iterCb(err);149        });150    }, err => callback(err, memo));151}152exports.default = (0, _awaitify2.default)(reduce, 4);153module.exports = exports.default;
basant307/AI_Governance_Project · CoolFace