How to use the when.settle 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 megamsys / varai / varai / nodes / registry.js View on Github external
var promises = [];
            files.forEach(function(fn) {
                var pkgfn = path.join(pm,fn,"package.json");
                try {
                    var pkg = require(pkgfn);
                    if (pkg['varai']) {
                        var moduleDir = path.join(pm,fn);
                        promises = promises.concat(loadNodesFromModule(moduleDir,pkg));
                    }
                } catch(err) {
                    if (err.code != "MODULE_NOT_FOUND") {
                        // TODO: handle unexpected error
                    }
                }
            });
            when.settle(promises).then(function(results) {
                var promises = [];
                results.forEach(function(result) {
                    promises = promises.concat(result.value);
                });
                resolve(promises);
            });
        },function(err) {
            resolve([]);
github node-red / node-red / packages / node_modules / @node-red / runtime / lib / storage / localfilesystem / projects / Project.js View on Github external
function checkProjectFiles(project) {
    var promises = [];
    var paths = [];
    for (var file in defaultFileSet) {
        if (defaultFileSet.hasOwnProperty(file)) {
            paths.push(file);
            promises.push(fs.stat(fspath.join(project.path,project.paths.root,file)));
        }
    }
    return when.settle(promises).then(function(results) {
        var missing = [];
        results.forEach(function(result,i) {
            if (result.state === 'rejected') {
                missing.push(paths[i]);
            }
        });
        return missing;
    }).then(function(missing) {
        // if (createMissing) {
        //     var promises = [];
        //     missing.forEach(function(file) {
        //         promises.push(util.writeFile(fspath.join(projectPath,file),defaultFileSet[file](project)));
        //     });
        //     return promises;
        // } else {
        return missing;
github ahlwong / dakota-cassandra / lib / user_defined_type.js View on Github external
lLogger.warn(missing);
        }
        
        // fix
        if ((mismatched.length > 0 || extra.length > 0 || missing.length > 0) && options.recreate) {
          recreate.call(self, callback);
        }
        else {
          var promises = [];
          if (mismatched.length > 0 && options.changeType) {
            promises = promises.concat(fixMismatched.call(self, nm_.map(mismatched, function(mismatch, index) { return mismatch.field })));
          }
          if (missing.length > 0 && options.addMissing) {
            promises = promises.concat(fixMissing.call(self, missing));
          }
          nmWhen.settle(promises).done(function(descriptors) {
            var success = true;
            nm_.each(descriptors, function(descriptor, index) {
              if (descriptor.state === 'rejected') {
                success = false;
                lLogger.error(descriptor.reason);
              }
            });
            if (success) {
              callback();
            }
            else {
              throw new lErrors.UserDefinedType.FixError('Fixing table schema failed: rejected promises.');
            }
          });
        }
github node-red / node-red / red / nodes.js View on Github external
var promises = [];
                        files.forEach(function(fn) {
                            var pkgfn = path.join(pm,fn,"package.json");
                            try {
                                var pkg = require(pkgfn);
                                if (pkg['node-red']) {
                                    var moduleDir = path.join(pm,fn);
                                    promises.push(loadNodesFromModule(moduleDir,pkg));
                                }
                            } catch(err) {
                                if (err.code != "MODULE_NOT_FOUND") {
                                    // TODO: handle unexpected error
                                }
                            }
                        });
                        when.settle(promises).then(function(results) {
                            var errors = [];
                            results.forEach(function(result) {
                                if (result.state == 'fulfilled' && result.value) {
                                    errors = errors.concat(result.value);
                                }
                            });
                            resolve(errors);
                        });
                    },function(err) {
                        resolve([]);
github megamsys / varai / varai / nodes / flows.js View on Github external
return when.promise(function(resolve) {
			events.emit("nodes-stopping");
			var promises = [];
			for (var n in nodes) {
				if (nodes.hasOwnProperty(n)) {
					try {
						var p = nodes[n].close();
						if (p) {
							promises.push(p);
						}
					} catch (err) {
						nodes[n].error(err);
					}
				}
			}
			when.settle(promises).then(function() {
				events.emit("nodes-stopped");
				nodes = {};
				resolve();
			});
		});
	},
github node-red / node-red / red / api / admin / nodes.js View on Github external
return;
        }
        var mod = req.params[0];
        try {
            var module = redNodes.getModuleInfo(mod);
            if (!module) {
                log.audit({event: "nodes.module.set",module:mod,error:"not_found"},req);
                return res.status(404).end();
            }

            var nodes = module.nodes;
            var promises = [];
            for (var i = 0; i < nodes.length; ++i) {
                promises.push(putNode(nodes[i],body.enabled));
            }
            when.settle(promises).then(function() {
                res.json(redNodes.getModuleInfo(mod));
            });
        } catch(err) {
            log.audit({event: "nodes.module.set",module:mod,enabled:body.enabled,error:err.code||"unexpected_error",message:err.toString()},req);
            res.status(400).json({error:err.code||"unexpected_error", message:err.toString()});
        }
    },
github ohmlabs / ohm / server / ghost / core / server / update-check.js View on Github external
return errors.rejectError(e);
            }

            return _.reduce(apps, function (memo, item) { return memo === '' ? memo + item : memo + ', ' + item; }, '');
        }).otherwise(errors.rejectError));
    ops.push(api.posts.browse().otherwise(errors.rejectError));
    ops.push(api.users.browse().otherwise(errors.rejectError));
    ops.push(nodefn.call(exec, 'npm -v').otherwise(errors.rejectError));

    data.ghost_version   = currentVersion;
    data.node_version    = process.versions.node;
    data.env             = process.env.NODE_ENV;
    data.database_type   = require('./models/base').client;
    data.email_transport = mailConfig && (mailConfig.options && mailConfig.options.service ? mailConfig.options.service : mailConfig.transport);

    return when.settle(ops).then(function (descriptors) {
        var hash             = descriptors[0].value,
            theme            = descriptors[1].value,
            apps             = descriptors[2].value,
            posts            = descriptors[3].value,
            users            = descriptors[4].value,
            npm              = descriptors[5].value,
            blogUrl          = url.parse(config().url),
            blogId           = blogUrl.hostname + blogUrl.pathname.replace(/\//, '') + hash.value;

        data.blog_id         = crypto.createHash('md5').update(blogId).digest('hex');
        data.theme           = theme ? theme.value : '';
        data.apps            = apps || '';
        data.post_count      = posts && posts.total ? posts.total : 0;
        data.user_count      = users && users.length ? users.length : 0;
        data.blog_created_at = users && users[0] && users[0].created_at ? moment(users[0].created_at).unix() : '';
        data.npm_version     = _.isArray(npm) && npm[0] ? npm[0].toString().replace(/\n/, '') : '';
github particle-iot / particle-cli / src / app / cloud.js View on Github external
(deviceIds) => {
				return when.settle(deviceIds.map(id => {
					return cloudLib.signalDevice(id, onOff).catch(err => {
						const errors = err && err.body && err.body.errors;
						return when.reject(errors || err);
					});
				}));
			}
		]).then(results => {
github node-red / node-red / packages / node_modules / @node-red / registry / lib / loader.js View on Github external
var m = module;
                                var n = node;
                                return function(nodeSet) {
                                    nodeFiles[m].nodes[n] = nodeSet;
                                    nodes.push(nodeSet);
                                }
                            })()));
                        } catch(err) {
                            //
                        }
                    }
                }
            }
        }
    }
    return when.settle(promises).then(function(results) {
        for (var module in nodeFiles) {
            if (nodeFiles.hasOwnProperty(module)) {
                if (!nodeFiles[module].err) {
                    registry.addModule(nodeFiles[module]);
                }
            }
        }
        return loadNodeSetList(nodes);
    });
}
github ahlwong / dakota-cassandra / lib / table.js View on Github external
if ((mismatched.length > 0 || extra.length > 0 || missing.length > 0) && options.recreate) {
          recreate.call(self, callback);
        }
        else {
          var promises = [];
          if (mismatched.length > 0 && options.recreateColumn) {
            promises = promises.concat(fixMismatched.call(self, nm_.map(mismatched, function(mismatch, index) { return mismatch.column })));
          }
          if (extra.length > 0 && options.removeExtra) {
            promises = promises.concat(fixExtra.call(self, extra));
            
          }
          if (missing.length > 0 && options.addMissing) {
            promises = promises.concat(fixMissing.call(self, missing));
          }
          nmWhen.settle(promises).done(function(descriptors) {
            var success = true;
            nm_.each(descriptors, function(descriptor, index) {
              if (descriptor.state === 'rejected') {
                success = false;
                lLogger.error(descriptor.reason);
              }
            });
            if (success) {
              callback();
            }
            else {
              throw new lErrors.Table.FixError('Fixing table schema failed: rejected promises.');
            }
          });
        }