Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

powerpromises

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

powerpromises

Easy use of Q promises

  • 0.0.2
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
0
decreased by-100%
Maintainers
1
Weekly downloads
 
Created
Source

#powerpromises

Library for simplify the use of javascript promises library q. It allow us to chain a group of calls to a function and execute them in parallel, sequentially, or a limited parallel group of calls. Take a look at the examples:

##Parallel execution

var Q = require('q'),
    pw = require('../powerpromises');

const NUMBER = 100;

var waitFn = function(number) {
    var df = Q.defer();

    console.log("start " + number + ".");
    setTimeout(function() {
        console.log("done " + number + ".");
        df.resolve(true);
    }, 3000);

    return df.promise;
}

var arrayOfNumbers = function() {
    var arr = [];
    for (var i=0; i<NUMBER; i++) {
        arr.push(i);
    }
    return arr;
};

pw.chainPromises(waitFn, arrayOfNumbers());


##Sequential execution

var Q = require('q'),
    pw = require('../powerpromises');

const NUMBER = 3;
const PARALLEL = 1;

var waitFn = function(number) {
    var df = Q.defer();

    console.log("start " + number + ".");
    setTimeout(function() {
        console.log("done " + number + ".");
        df.resolve(true);
    }, 3000);

    return df.promise;
}

var arrayOfNumbers = function() {
    var arr = [];
    for (var i=0; i<NUMBER; i++) {
        arr.push(i);
    }
    return arr;
};

pw.chainPromises(waitFn, arrayOfNumbers(), PARALLEL);

##Sequential execution with limited grouped calls in parallel

var Q = require('q'),
    pw = require('../powerpromises');

const NUMBER = 100;
const PARALLEL = 5;

var waitFn = function(number) {
    var df = Q.defer();

    console.log("start " + number + ".");
    setTimeout(function() {
        console.log("done " + number + ".");
        df.resolve(true);
    }, 3000);

    return df.promise;
}

var arrayOfNumbers = function() {
    var arr = [];
    for (var i=0; i<NUMBER; i++) {
        arr.push(i);
    }
    return arr;
};

pw.chainPromises(waitFn, arrayOfNumbers(), PARALLEL);

##Sequential execution with limited grouped calls in parallel and error management

var Q = require('q'),
    pw = require('../powerpromises');

const NUMBER = 25;
const PARALLEL = 5;

var waitFn = function(number) {
    var df = Q.defer();

    console.log("start " + number + ".");
    setTimeout(function() {
        if ( number % 2 === 0) {
            console.log("done " + number + ".");
            df.resolve({ number: number, status: true });
        } else {
            console.log("fail " + number + ".");
            df.reject({ number: number, status: false });
        }
    }, 3000);

    return df.promise;
}

var arrayOfNumbers = function() {
    var arr = [];
    for (var i=0; i<NUMBER; i++) {
        arr.push(i);
    }
    return arr;
};

pw.chainPromises(waitFn, arrayOfNumbers(), PARALLEL).then(function(results) {
    console.log(results);
});

Keywords

FAQs

Package last updated on 30 Sep 2014

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc