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

promise_mtd

Package Overview
Dependencies
Maintainers
1
Versions
26
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

promise_mtd

Set of methods allowing simplify work with Promises in cycle. Methods: forEach, map, while, transform, parallel

  • 0.0.2
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
43
increased by72%
Maintainers
1
Weekly downloads
 
Created
Source

promise_mtd

Set of methods allowing simplify work with Promises in cycle. Implementation forEach and map for working with array data unto whom need applied asynchronously function Method transform allows asynchronous iterations with array such as map, but also it can skip unnecessary data Implementation cycle while for using with Promise Method parallel allows run concurrently promises such as method Promise.all, but with limit.

npm i promise_mtd -S

foreach(Array, Function(el, index)) || forEach(Array, Function(el, index))

Foreach over promises serially

const promiseMtd = require('promise_mtd');
void async function () {
  await promiseMtd.forEach([ 300, 200, 100], async function (el, i) {
    return new Promise((resolve, reject) => {
      setTimeout(function() {
        console.log(el);
        resolve();
      }, el+i);
    });
  });
}();

map(Array, Function(el, index): Promise)

Map over promises serially

const promiseMtd = require('promise_mtd');
void async function () {
  let res = await promiseMtd.map([ 300, 200, 100], async function (el, i) {
    return new Promise((resolve, reject) => {
      setTimeout(function() {
        resolve(el*2);
      }, el*2);
    });
  });
  console.log(res); // [ 600, 400, 200 ]
}();

parallel(Array, Function(el, index))

Equalent Promise.all, but with limit

const promiseMtd = require('promise_mtd');
void async function() {
  try {
    await promiseMtd.parallel([ 3000, 3000, 3000, 2000, 2000, 2000, 1000], 3, async function(t, i) {
      return new Promise((resolve) => {
        // if (i === 4) {
        //   throw new Error('stop');
        // }
        setTimeout(() => {
          console.log(t);
          resolve();
        }, t);
      });
    });
  } catch (err) {
    console.log('Raise', err);
  }
}();

transform(Array, Function(el, index))

Iterate array and filter over promises

const promiseMtd = require('promise_mtd');
void async function() {
  let res = await promiseMtd.transform([ 1, 2, 3, 4 ], function (el, i) {
    if (el <= 2) {
      return new Promise((resolve) => {
        setTimeout(() => resolve({ el, i }), 1000);
      });
    }
  });
  console.log(res); // [ { el: 1, i: 0 }, { el: 2, i: 1 } ]
}();

while(condition: Function(): Boolean, Function)

While over promises serially

const promiseMtd = require('promise_mtd');
void async function() {
  let count = 5;
  await promiseMtd.while(() => count <= 0, async () => {
    return new Promise((resolve, reject) => {
      setTimeout(() => {
        console.log(count);
        count--;
        resolve();
      }, count * 1000);
    });
  })
  console.log(count); // 0
}();

Keywords

FAQs

Package last updated on 01 Oct 2019

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