1.0.2 • Published 3 months ago

stack-promises v1.0.2

Weekly downloads
2
License
MIT
Repository
github
Last release
3 months ago

stack-promises

npm npm bundle size

A stack of tasks that are executed one by one, but the result is taken from the last. Identical functions on the stack (check by reference) are executed only once.

Install

npm

npm install stack-promises

yarn

yarn add stack-promises

Usage

import creteStackPromises from 'stack-promises';

const stackPromises = creteStackPromises();

stackPromises.add(() => Promise.resolve(1));
stackPromises.add(() => Promise.resolve(2));

stackPromises().then(data => {
  console.log(data); /// 2
});

Execute after add

import creteStackPromises from 'stack-promises';

const stackPromises = creteStackPromises();

stackPromises
  .add(() => Promise.resolve(1))() // execute
  .then(data => {
    console.log(data); // 1
  });
stackPromises
  .add(() => Promise.resolve(2))() // execute
  .then(data => {
    console.log(data); // 2
  });

stackPromises().then(data => {
  console.log(data); // 2
});

Add after execute

import creteStackPromises, { isPromiseIsNotActualError } from 'stack-promises';
import delayPromise from 'promise-delay';

const stackPromises = creteStackPromises();

let checkQue = 0;
const request1 = () =>
  delayPromise(3000, 1).finally(() => {
    checkQue += 1;
  });
const resultAfter1 = stackPromises.add(request1)();

const request2 = () =>
  delayPromise(1000, 2).finally(() => {
    checkQue *= 2;
  });
const resultAfter2 = stackPromises.add(request2)();

Promise.allSettled([resultAfter1, resultAfter2]).then(([{ reason }, { value }]) => {
  isPromiseIsNotActualError(reason); // true
  value; // 2
  checkQue; // 2
  // request1 called 1 times
  // request2 called 1 times
});

Chaining

stackPromises.add(() => Promise.resolve(1)).add(() => Promise.resolve(2));

Run tests

npm test

Maintainer

Krivega Dmitriy

Contributing

Contributions, issues and feature requests are welcome!Feel free to check issues page. You can also take a look at the contributing guide.

📝 License

Copyright © 2020 Krivega Dmitriy. This project is MIT licensed.

1.0.2

3 months ago

1.0.1

7 months ago

1.0.0

7 months ago

0.0.11

1 year ago

0.0.12

1 year ago

0.0.13

1 year ago

0.0.10

2 years ago

0.0.9

2 years ago

0.0.8

2 years ago

0.0.7

3 years ago

0.0.6

3 years ago

0.0.5

3 years ago

0.0.4

4 years ago

0.0.3

4 years ago

0.0.2

4 years ago

0.0.1

4 years ago