How to use the bluebird.reduce function in bluebird

To help you get started, we’ve selected a few bluebird 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 linode / manager / packages / manager / src / store / tagImportDrawer / index.ts View on Github external
export const addTagsToEntities: ImportGroupsAsTagsThunk = () => (
  dispatch,
  getState
) => {
  dispatch(importTagsActions.started());
  const entities = getEntitiesWithGroupsToImport(getState());

  const linodeAccumulator = createAccumulator('linode', dispatch);
  const domainAccumulator = createAccumulator('domain', dispatch);

  Bluebird.join(
    Bluebird.reduce(entities.linodes, linodeAccumulator, {
      success: [],
      errors: []
    }),
    Bluebird.reduce(entities.domains, domainAccumulator, {
      success: [],
      errors: []
    }),
    dispatch,
    handleAccumulatedResponsesAndErrors
  )
    .then((totalErrors: TagError[]) => {
      if (isEmpty(totalErrors)) {
        storage.hasImportedGroups.set();
      }
    })
    .catch(() =>
      dispatch(
        // Errors from individual requests will be accumulated and passed to .then(); hitting
        // this block indicates something went wrong with .reduce() or .join().
        // It's unclear under what circumstances this could ever actually fire.
github linode / manager / packages / manager / src / store / backupDrawer / index.ts View on Github external
export const enableAllBackups: EnableAllBackupsThunk = () => (
  dispatch,
  getState
) => {
  const { entities } = getState().__resources.linodes;

  const linodesWithoutBackups = entities.filter(
    linode => !linode.backups.enabled
  );

  dispatch(handleEnable());
  Bluebird.reduce(linodesWithoutBackups, gatherResponsesAndErrors, {
    success: [],
    errors: []
  })
    .then(response => {
      if (response.errors && !isEmpty(response.errors)) {
        dispatch(handleEnableError(response));
      } else {
        dispatch(handleEnableSuccess(response.success));
      }
      dispatch(updateMultipleLinodes(response.success));
      // GA Event
      sendBackupsEnabledEvent(
        `Enabled backups for ${response.success.length} Linodes`
      );
    })
    .catch(() =>
github TryGhost / Ghost / core / server / data / migration / update.js View on Github external
sequence = function sequence(tasks, modelOptions, logger) {
        // utils/sequence.js does not offer an option to pass cloned arguments
        return Promise.reduce(tasks, function (results, task) {
            return task(_.cloneDeep(modelOptions), logger)
                .then(function (result) {
                    results.push(result);
                    return results;
                });
        }, []);
    },
    updateDatabaseSchema,
github OpenTMI / opentmi / app / tools / utilization.js View on Github external
const statistics = _.cloneDeep(this._accumulator);
    // calculate utilizations
    let utilization = (statistics.summary.allocations.time / duration) * 100;
    statistics.summary.allocations.utilization = utilization;
    utilization = (statistics.summary.maintenance.time / duration) * 100;
    statistics.summary.maintenance.utilization = utilization;
    // calculate day based utilizations..
    const reducer = (accumulator, date) => {
      const obj = accumulator.dates[date];
      utilization = (obj.allocations.time / duration) * 100;
      obj.allocations.utilization = utilization;
      utilization = (obj.maintenance.time / duration) * 100;
      obj.maintenance.utilization = utilization;
      return new Promise(resolve => process.nextTick(() => resolve(accumulator)));
    };
    return Promise.reduce(Object.keys(statistics.dates), reducer, statistics);
  }
  /*
github LiskHQ / lisk-sdk / test / unit / sql / rounds.js View on Github external
.then(function (delegates) {
						delegates_rewards = delegates;
						return Promise.reduce(blocks, function (blocks, b) {
							var pk;
							pk = b.generatorPublicKey.toString('hex');
							if (blocks[pk]) {
								blocks.rewards += Number(b.reward);
							} else {
								blocks[pk] = {
									pk: pk,
									rewards: Number(b.reward)
								}
							}
							return blocks;
						}, {})
						.then(function (blocks) {
							expect(delegates_rewards).to.deep.equal(blocks);
						});
					});
github contentful / contentful-import / lib / tasks / get-destination-data.js View on Github external
function batchedIdQuery (environment, type, ids) {
  const method = METHODS[type].method
  const entityTypeName = METHODS[type].name
  return Promise.reduce(getIdBatches(ids), (fullResponse, batch) => {
    return environment[method]({
      'sys.id[in]': batch,
      limit: batch.split(',').length
    })
      .then((response) => {
        fullResponse = [
          ...fullResponse,
          ...response.items
        ]
        logEmitter.emit('info', `Fetched ${fullResponse.length} of ${response.total} ${entityTypeName}`)
        return fullResponse
      })
  }, [])
}
github city41 / node-sql-fixtures / lib / fixture-generator.js View on Github external
var mainPromise = bluebird.all(fkQueries).then(function createRecords() {
    return bluebird.reduce(prioritized, function(buildingFinalResult, priorityLevel) {
      priorityLevel = resolveDependencies(buildingFinalResult, priorityLevel);
      priorityLevel = unescape(priorityLevel);
      var priorityLevelPromises = insertRecords(knexInst, priorityLevel, options.unique, options.showWarning);
      return bluebird.all(priorityLevelPromises).then(function(levelResults) {
        return addToFinalResult(buildingFinalResult, levelResults, withSpecIds);
      });
    }, {}).then(function(finalResult) {
      return stripSpecIds(finalResult);
    });
  });
github zhansingsong / create-fes / packages / fes-scripts / config / plugins / sprites / core.js View on Github external
function applyGroupBy(opts, images) {
	opts.logger('Applying the groups...');

	return Promise.reduce(opts.groupBy, (images, groupFn) => {
		return Promise.map(images, (image) => {
			return groupFn(image)
				.then(group => {
					image.groups.push(group);
					return image;
				})
				.catch(() => image);
		});
	}, images).then(images => [opts, images]);
}
github zhr85210078 / node-mongodb-es-connector / lib / promise / mongoPromise.js View on Github external
getGridFsFileByMetaDataMainId(mongoUrl, mainId).then(function (files) {
            var resultArry = [];
            resultArry[0] = "success";
            var attachments_SizeByte = 0;
            if (files.length > 0) {
                Promise.reduce(files, function (total, item) {
                    return new Promise(function (resolve, reject) {
                        if ((attachmentSize && item.length < attachmentSize) || (!attachmentSize && item.length < maxAttachmentSize)) {
                            getGridFsStreams(mongoUrl, item._id).then(function (result) {
                                if (result) {
                                    var resultObj = {};
                                    resultObj.filename = item.filename;
                                    resultObj.data = result;
                                    resultArry.push(resultObj);
                                    attachments_SizeByte += item.length;
                                    return resolve(resultArry);
                                } else {
                                    return reject('Fail get gridfs streams (' + currentData + '/' + mongodataCount + '), DocId: ' + mainId + ', fileId: ' + item._id.toString());
                                }
                            }).catch(function (err) {
                                return reject(err);
                            });
github WorldBank-Transport / ram-backend / app / s3 / utils.js View on Github external
export async function getLocalFilesInDir (dir) {
  const files = await fs.readdir(dir);

  return Promise.reduce(files, async (acc, file) => {
    const name = dir + '/' + file;
    const stats = await fs.stat(name);

    return stats.isDirectory()
      ? acc.concat(await getLocalFilesInDir(name))
      : acc.concat(name);
  }, []);
}