How to use the async.reduce function in async

To help you get started, we’ve selected a few async examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github jbhannah / amperize / lib / amperize.js View on Github external
// convert all of the img elements first so that we can perform lengthy
    // network requests in parallel before sequentially traversing the DOM
    if (self.config['amp-img']) {
        var imgTest = function(elem) {
            return elem.name === 'img' && elem.attribs.src;
        }
        var imgElems = domutils.findAll(elem => imgTest(elem), data);
        var imgTasks = imgElems.map(elem => amperizeImageElem(elem));
        await async.parallelLimit(imgTasks, 10);
    }

    // sequentially traverse the DOM
    async.reduce(data, html, function reduce(html, element, step) {
        var children;

        if (/(style|script|textarea|link)/.test(element.name)) {
            return step(null, html);
        }

        function close(error, html) {
            html += helpers.close(element);
            step(null, html);
        }

        function enter() {
            children = element.children;
            html += helpers[element.type](element);

            if (!children || !children.length) {
github dadi / api / dadi / lib / model / index.js View on Github external
finalResult.results = this.formatResultSetForOutput(finalResult.results)

          done(err, finalResult)
        })
      } else {
        // Prepare result set for output
        results.results = this.formatResultSetForOutput(results.results)

        done(err, results)
      }
    })
  }

  // Run any `beforeGet` hooks
  if (this.settings.hooks && this.settings.hooks.beforeGet) {
    async.reduce(this.settings.hooks.beforeGet, query, (current, hookConfig, callback) => {
      var hook = new Hook(hookConfig, 'beforeGet')

      Promise.resolve(hook.apply(current, this.schema, this.name, req)).then(newQuery => {
        callback(null, newQuery)
      }).catch(err => {
        callback(hook.formatError(err))
      })
    }, (err, finalQuery) => {
      if (err) {
        return done(err, null)
      }

      databaseGet(finalQuery)
    })
  } else {
    databaseGet(query)
github alex94cp / jsonapify / lib / accessors / refs.js View on Github external
Refs.prototype.serialize = function(object, response, cb) {
	var resource = this._resource;
	var ids = _.get(object, this._thisProperty);
	async.reduce(ids, [], function(rels, id, cb) {
		resource.findOne({ _id: id }, function(err, relObject) {
			if (err || !relObject) return cb(err);
			resource.serialize(relObject, response, function(err, relData) {
				if (err) return cb(err);
				var data = _.pick(relData, 'type', 'id');
				response.include(data.type, data.id, relData);
				rels.push(data);
				cb(null, rels);
			});
		});
	}, function(err, rels) {
		err ? cb(err) : cb(null, { data: rels });
	});
};
github electron-userland / electron-json-storage / lib / storage.js View on Github external
exports.getMany = function(keys, options, callback) {
  if (_.isFunction(options)) {
    callback = options;
    options = {};
  }

  options = options || {};
  callback = callback || _.noop;

  async.reduce(keys, {}, function(reducer, key, callback) {
    exports.get(key, options, function(error, data) {
      if (error) {
        return callback(error);
      }
      return callback(null, _.set(reducer, key, data));
    });
  }, callback);
};
github aliaksandr-master / node-verifier-schema / lib / Schema.js View on Github external
_validationInner: function (value, options, done) {
		var that = this;

		if (this.isArray) {
			if (!_.isArray(value)) {
				done(new Schema.ValidationError('type', 'array', this.path, value));
				return;
			}

			async.reduce(value, null, function (_1, value, done) {
				that._validateFields(value, options, done);
			}, done);

			return;
		}

		that._validateFields(value, options, done);
	},
github racker / node-swiz / lib / valve.js View on Github external
validator(memo, baton, function(err, result) {
      var message;
      if (err) {
        if (err.hasOwnProperty(message)) {
          message = err.message;
        } else {
          message = err;
        }
        callback(message);
      } else {
        callback(null, result);
      }
    });
  }

  async.reduce(funs, value, _reduce, callback);
}
github n1k0 / stpackages / lib / db.js View on Github external
fs.readdir(dataDir, function(err, files) {
    if (err) return cb(err);
    async.reduce(files, [], function(memo, file, cb) {
      if (err) return cb(err);
      fs.readFile(path.join(dataDir, file), function(err, json) {
        if (err) return cb(err);
        try {
          memo.push(JSON.parse(json));
        } catch (err) {
          return cb('JSON parse error: ' + err);
        }
        cb(null, memo);
      });
    }, cb);
  });
}
github patrickarlt / acetate / lib / Transformer.js View on Github external
}

        this.logger.info(
          `Transformed ${pages.length} pages in ${this.logger.timeEnd(
            _masterTimer
          )}`
        );

        resolve(pages);
      };

      const iterator = (pages, transformer, callback) => {
        transformer(pages, callback);
      };

      async.reduce(this._transformers, pages, iterator, done);
    });
  }
github macalinao / preston / lib / model.js View on Github external
Model.prototype.applyTransforms = function(req, doc, cb) {
  async.reduce(this.transformers, doc.toObject(), function(memo, tf, next) {
    if (tf.length === 2) {
      tf(req, memo);
      next(null, memo);
    } else {
      tf(req, memo, function(err) {
        return next(err, memo);
      });
    }
  }, function(err, result) {
    cb(err, result);
  });
};
github tes / bosco / src / RunListHelper.js View on Github external
function resolveDependencies(repoList, resolved, cb) {
    async.reduce(repoList, resolved, function (memo, repo, cb2) {
      if (_.includes(memo, repo)) {
        return cb2(null, memo);
      }
      memo.push(repo);
      getRunConfig(bosco, repo, watchRegex, function (err, svcConfig) {
        if (err) {
          bosco.error('Unable to retrieve config from github for: ' + repo.cyan + ' because: ' + err.message);
          return cb2(null, memo);
        }
        if (!svcConfig) {
          return cb2(null, memo);
        }
        configs[repo] = svcConfig;
        if (svcConfig && svcConfig.service && svcConfig.service.dependsOn) {
          resolveDependencies(svcConfig.service.dependsOn, memo, cb2);
        } else {