MCPcopy Create free account
hub / github.com/apache/cassandra-nodejs-driver / mapSeries

Function mapSeries

lib/utils.js:790–832  ·  view source on GitHub ↗

* @param {Array} arr * @param {Function} fn * @param {Function} [callback]

(arr, fn, callback)

Source from the content-addressed store, hash-verified

788 * @param {Function} [callback]
789 */
790function mapSeries(arr, fn, callback) {
791 if (!Array.isArray(arr)) {
792 throw new TypeError('First parameter must be an Array');
793 }
794 callback = callback || noop;
795 const length = arr.length;
796 if (length === 0) {
797 return callback(null, []);
798 }
799 const result = new Array(length);
800 let index = 0;
801 let sync;
802 invoke(0);
803 if (sync === undefined) {
804 sync = false;
805 }
806
807 function invoke(i) {
808 fn(arr[i], function mapItemCallback(err, transformed) {
809 result[i] = transformed;
810 next(err);
811 });
812 }
813
814 function next(err) {
815 if (err) {
816 return callback(err);
817 }
818 if (++index === length) {
819 return callback(null, result);
820 }
821 if (sync === undefined) {
822 sync = true;
823 }
824 const i = index;
825 if (sync) {
826 return process.nextTick(function () {
827 invoke(i);
828 });
829 }
830 invoke(index);
831 }
832}
833
834/**
835 * @param {Array.<Function>} arr

Callers

nothing calls this directly

Calls 1

invokeFunction · 0.85

Tested by

no test coverage detected