GithubHelp home page GithubHelp logo

promise_mtd's Introduction

promise_mtd

Set of methods allowing to simplify work with promises in cycle. Implementation of forEach and map for working with array data when it's needed to apply asynchronous function to each element. Method transform allows to iterate asynchronously over an array similar to map, but also it can skip unnecessary data. Implementation of cycle while for using with promise. Method parallel allows to run concurrently promises such as method Promise.all, but with limit. The library has no dependencies ๐Ÿ˜€.

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
}();

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.