https://github.com/trapcodeio/stack-promise
Stack Promises in functions to run later.
https://github.com/trapcodeio/stack-promise
Last synced: 12 months ago
JSON representation
Stack Promises in functions to run later.
- Host: GitHub
- URL: https://github.com/trapcodeio/stack-promise
- Owner: trapcodeio
- Created: 2022-01-31T14:07:30.000Z (about 4 years ago)
- Default Branch: main
- Last Pushed: 2023-01-10T13:34:59.000Z (about 3 years ago)
- Last Synced: 2025-03-20T05:34:54.479Z (12 months ago)
- Language: TypeScript
- Size: 7.81 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
Awesome Lists containing this project
README
# Stack JavaScript Promise
This simple library provides a more semantic version to the code below:
```typescript
const tasks = [];
// Stack promises
tasks.push(() => Promise.resolve("promise1"))
tasks.push(() => Promise.resolve("promise2"))
// Then run the tasks
await Promise.all(tasks.map(task => task()))
```
Using this library, the code above can be written as:
```typescript
import { StackedPromise } from "stack-promise";
const tasks = StackedPromise();
tasks.push(() => Promise.resolve("promise1"))
tasks.push(() => Promise.resolve("promise2"))
// Then run the tasks
await tasks.run()
```
## Installation
```shell
npm i stacked-promise
# or
yarn add stacked-promise
```
## StackPromise Class
| Property/Method | Description |
|-----------------|-------------------------------------------------------------------------|
| `stack` | The array holding the stack of promises |
| `push()` | Push a function that returns a promise |
| `unstack()` | Unstack promises by running each function |
| `run()` | Run the tasks |
| `concat()` | Concatenate two stacks |
| `some()` | Run every task but stop when any passes the validation function passed. |
| `every()` | Run every task but stop when any fails the validation function passed. |
### push()
Push a function that returns a promise
```typescript
const tasks = StackedPromise();
tasks.push(() => Promise.resolve("promise1"))
tasks.push(() => Promise.resolve("promise2"))
console.log(tasks.stack.length) // 2
```
### unstack()
Unstack promises by running each function
```typescript
const tasks = StackedPromise();
tasks.push(() => Promise.resolve("promise1"))
tasks.push(() => Promise.resolve("promise2"))
tasks.unstack() // [Promise, Promise]
```
### run()
Run the tasks
```typescript
const tasks = StackedPromise();
tasks.push(() => Promise.resolve("promise1"))
tasks.push(() => Promise.resolve("promise2"))
await tasks.run() // ["promise1", "promise2"]
```
### concat()
Concatenate two stacks
```typescript
const tasks = StackedPromise();
tasks.push(() => Promise.resolve("promise1"))
const tasks2 = StackedPromise();
tasks2.push(() => Promise.resolve("promise2"))
tasks2.push(() => Promise.resolve("promise3"))
tasks.concat(tasks2)
console.log(tasks.stack.length) // 3
```
### some()
Run every task but stop when any passes the validation function passed.
```typescript
const tasks = StackedPromise();
tasks.push(() => Promise.resolve(true))
tasks.push(() => Promise.resolve(true))
await tasks.some((result) => result === true) // true
await tasks.some((result) => result === false) // false
```
### every()
Run every task but stop when any fails the validation function passed.
```typescript
const tasks = StackedPromise();
tasks.push(() => Promise.resolve(true))
tasks.push(() => Promise.resolve(true))
await tasks.every((result) => result === true) // true
await tasks.every((result) => result === false) // false
```