How to use the when.map 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 flint-bot / flint / lib / flint.js View on Github external
.then(webhooks => {

        // remove all webhooks on stop
        if(!flint.options.removeWebhooksOnStart) {
          var webhooksToRemove = _.filter(webhooks, webhook => {
            return (webhook.name == u.base64encode(flint.options.webhookUrl.split('/')[2] + ' ' + flint.email));
          });

          if(webhooksToRemove instanceof Array && webhooksToRemove.length > 0) {
            return when.map(webhooksToRemove, webhook => flint.spark.webhookRemove(webhook.id))
              .then(() => when(true))
              .catch(() => when(true));
          } else {
            return when(true);
          }
        }

        // else, only remove webhooks this app created
        else {
          return when.map(webhooks, webhook => flint.spark.webhookRemove(webhook.id))
            .then(() => when(true))
            .catch(() => when(true));
        }

      });
  } else {
github ncarlier / nunux-keeper / server / connectors / twitter.js View on Github external
.then(function(_doc) {
        // Create document(s)
        if (_.isArray(_doc)) {
          return when.map(_doc, function(item) {
            return Document.persist(item);
          });
        } else {
          return Document.persist(_doc);
        }
      });
    }, function(err) {
github ncarlier / nunux-keeper / server / api / document.js View on Github external
var deleteDocument = function(id) {
      return Document.findById(id).exec()
      .then(function(doc) {
        if (!doc) return when.reject(new errors.NotFound('Document not found.'));
        if (doc.owner === req.user.uid) {
          return Document.del(doc);
        } else {
          return when.reject(new errors.Forbidden());
        }
      });
    };

    if (ids) {
      // Delete defined ids
      when.map(ids, deleteDocument).then(function() {
        res.send(205);
      }, next);
    } else {
      logger.info('Emptying trash bin of %s ...', req.user.uid);
      // Empty trash bin category
      Document.find({ owner: req.user.uid, categories: 'system-trash' }).exec()
      .then(function(docs) {
        return when.map(docs, function(doc) {
          return Document.del(doc);
        });
      })
      .then(function() {
        res.send(205);
      }, next);
    }
  }
github cujojs / cram / lib / compile.js View on Github external
function compile (parentCtx, ids, isExcluded) {
			var toAmd, fetch, compileDeps;

			toAmd = amdFromPlugin(readFile, parentCtx);
			fetch = partial(fetchSource, readFromSource, compile, toAmd, isExcluded);
			compileDeps = partial(processDeps, compile, isExcluded);

			return when.map(ids, function (id) {
				var ctx, absId;

				// normalize
				absId = parentCtx.toAbsId(id);

				if (id in pseudoModules) return { absId: absId };

				ctx = getCtx(absId, parentCtx.config);

				// check if this module is excluded
				if (isExcluded(absId)) {
					ctx.source = '';
					status.info('Excluded ' + absId);
					return ctx;
				}
github static-dev / spike-rooftop / lib / index.js View on Github external
.then((template) => {
      return W.map(data, (item) => {
        addDataTo = Object.assign(addDataTo, { item: item })
        compiler.resourcePath = filePath

        // webpack context is used by default in spike for plugins, so we need
        // to mock it so that plugins dont crash
        const fakeContext = { addDependency: (x) => x, resourcePath: filePath }
        const options = loader.parseOptions.call(fakeContext, this.util.getSpikeOptions().reshape)

        // W.map fires events as quickly as possible, so the locals will be
        // swapped for the last item unless bound to the result function
        return reshape(options)
          .process(template)
          .then(((locals, res) => {
            const html = res.output(locals)
            compilation.assets[ct.template.output(item)] = {
              source: () => html,
github cubejs / cluster2 / lib / cache-socket / cache-socket.js View on Github external
hosts = undefined;
    }

    ports = ports || [];
    if (ports.length < _this.sockets.length) {
        var error = new Error('The number of ports does not match the number of sockets');
        if (cb) {
            cb(error);
        }
        _this.emit('error', error);
        return _this;
    }

    hosts = hosts || [];

    when.map(_.range(_this.sockets.length), function (ith) {
        var tillConnect = when.defer();
        _this.sockets[ith].connect(ports[ith], hosts[ith], function (error) {
            if (error) {
                tillConnect.reject(error);
            }else {
                tillConnect.resolve(_this.sockets[ith]);
            }
        });
        return tillConnect.promise;
    }).then(function (resolved) {
        if (cb) {
            cb(null);
        }
        _this.emit('connect');
    }).otherwise(function (error) {
        if (cb) {
github ncarlier / nunux-keeper / server / connectors / pocket.js View on Github external
var processData = function(user, data) {
    if (!data.list) {
      return when.reject('Pocket data not well formed.');
    }
    logger.debug('Importing %d Pocket item(s)...', Object.keys(data.list).length);
    return when.map(_.values(data.list), function(item) {
      var doc = {
        content:     item,
        contentType: 'application/json',
        owner:       user.uid
      };
      return Document.extract(doc)
      .then(function(_doc) {
        return Document.persist(_doc);
      });
    }, function(err) {
      logger.error('ERROR during processing Pocket data.', err);
      return when.reject(err);
    });
  };
github cambecc / earth / server / gfs-update.js View on Github external
function processCycle(cycle, forecasts) {
    log.info(JSON.stringify(cycle) + " " + forecasts);

    var products = forecasts.map(function(forecastHour) {
        return gfs.product(opt.productType, cycle, forecastHour);
    });
    var downloads = when.map(products, download_throttled);
    var extracted = when.map(downloads, extractLayers_throttled);
    var pushed = when.map(extracted, pushLayers);

    return pushed.then(function() {
        log.info("batch complete");
    });
}
github flint-bot / flint / lib / factory / trigger.js View on Github external
      const getFiles = fileUrls => when.map(
        fileUrls,
        fileUrl => this.flint.spark.contentGet(fileUrl).catch(err => when(false)),
      ).then(results => when(_.compact(results)));
github chenckang / react-json-pretty / example / node_modules / stylus-loader / lib / pathcache.js View on Github external
return when.map(Object.keys(newPaths), function(key) {
        var found = newPaths[key] && newPaths[key].path;
        if (found) {
          return when.map(found, nestResolve);
        }
      });
    })