How to use the when.reduce function in when

To help you get started, we’ve selected a few when 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 cujojs / cola / data / Rest.js View on Github external
patch: function(patch) {
			if(!this._shadow) {
				return;
			}

			var metadata = this.metadata;
			this._shadow = metadata.patch(this._shadow, patch);

			var identify = this.metadata.id;
			var client = this._client;
			var seen = {};

			// Need a better strategy here for arrays.  Using ids
			// would work out fine regardless.
			return when.reduce(patch, function(previousRequest, change) {
				/*jshint maxcomplexity:6*/
				var entity, segments;
				var p = change.path;

				segments = p.split(change.path);
				p = segments[0];

				if(seen[p]) {
					return;
				}

				seen[p] = 1;

				if(segments.length === 1) {
					if(change.op === 'add') {
						entity = metadata.getValue(data, p);
github scripted-editor / scripted / client / scripts / scripted / editor / save-hooks.js View on Github external
function preSaveHook(editor) {
		sort();

		//Call each of the handlers one by one in sequence until one of them
		//rejects. The preSaveHook returns a promise that rejects if any
		//of the promises where rejected.
		return when.reduce(preSaveHandlers,
			function (acc, handler, i) {
				return when(acc, function () {
					//console.log('handle index: '+i);
					return handler(editor, editor.getFilePath());
				});
			},
			null //IMPORTANT: may be anything except undefined...
				// because reduce behaves differently if no inital value is suplied!
		);
	}
github cambecc / air / server.js View on Github external
function doP160(date) {
    // return a promise for a boolean which is false if data was processed, and true if data was not available
    // i.e., true == we are done.
    // CONSIDER: This function's behavior is subtle and confusing. Improve.
    var promises = [doP160Page(1, date), doP160Page(2, date)];
    return when.reduce(
        promises,
        function(current, value) {
            return current && !value;
        },
        true);
}
github static-dev / spike-datocms / lib / index.js View on Github external
run(compilation, done) {
    if (this.addDataTo.dato && !this.aggressiveRefresh) return done()
    return W.all([
      this.client.site.find(),
      W.reduce(
        this.models,
        (memo, model) => {
          // format options
          const options = { allPages: true }
          const params = { version: this.drafts ? 'latest' : 'published' }
          if (model.ids) {
            params['filter[ids]'] = model.ids
          }
          if (model.query) {
            params['filter[query]'] = model.query
          }
          if (model.offset) {
            params['page[offset]'] = model.offset
          }
          if (model.limit) {
            params['page[limit]'] = model.limit
github teambition / then.js / benchmark / when.js View on Github external
.then(function () { // 串行 list 队列
      return when.reduce(list, function (x, i) {
        return task(i);
      }, 1);
    })
    .then(function () { // 并行 tasks 队列
github particle-iot / particle-cli / src / cmd / webhook.js View on Github external
return api.listWebhooks().then(hooks => {
							console.log('Found ' + hooks.length + ' hooks registered\n');
							return when.reduce(hooks, (ignore, hook) => {
								console.log('deleting ' + hook.id);
								return api.deleteWebhook(hook.id);
							});
						});
					}
github tastejs / todomvc / examples / cujo / node_modules / cola / network / strategy / compose.js View on Github external
return function (source, dest, data, type, api) {

			return when.reduce(strategies,
				function(result, strategy) {
					var strategyResult = strategy(source, dest, data, type, api);
					return api.isCanceled()
						? when.reject(strategyResult)
						: strategyResult;
				},
				data
			).then(propagateSuccess, propagateSuccess);

		}
github ncarlier / nunux-keeper / server / helpers / files.js View on Github external
.then(function(list) {
        var subs = [];
        list.forEach(function(item) {
          subs.push(getChrootDiskUsage(path.join(loc, item)));
        });
        if (subs.length) {
          return when.reduce(subs, function(sum, value) {
            return sum += value;
          });
        } else return when.resolve(stats.size);
      });
    } else {
github shama / stylus-loader / lib / pathcache.js View on Github external
function reduceResolvers(resolvers, context, path) {
  return when
    .reduce(resolvers, function(result, resolver) {
      return result ? result : resolver(context, path);
    }, undefined);
}
github tastejs / todomvc / labs / architecture-examples / cujo / lib / cola / network / strategy / compose.js View on Github external
return function (source, dest, data, type, api) {

			when.reduce(strategies,
				function(result, strategy) {
					var strategyResult = strategy(source, dest, data, type, api);
					return api.isCanceled()
						? when.reject(strategyResult)
						: strategyResult;
				},
				data
			).always(function(result) { return result });

		}