Socket
Book a DemoInstallSign in
Socket

@brianmcallister/sequential-promise

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

@brianmcallister/sequential-promise

Run Promises that depend on each other sequentially

latest
Source
npmnpm
Version
1.0.1
Version published
Weekly downloads
257
25.37%
Maintainers
1
Weekly downloads
 
Created
Source

@brianmcallister/sequential-promise

codecov CircleCI npm version

Run Promises that depend on each other sequentially

sequential-promise allows for running a series of promises sequentially, where each promise in the list depends on the previous promise having settled. There are two functions in this package, one that mirrors the behavior of Promise#all, and one mirroring Promise#allSettled (but which does not require Promise#allSettled to exist in your environment).

Table of contents

Installation

npm install @brianmcallister/sequential-promise
⇡ Top

Usage

The main concept to understand here is that you'll need to create an array of functions that create Promises, not an array of Promises (if you're using TypeScript, then the compiler will yell at you if you pass Promise<unknown>[]).

The functions in this package iterate over the array of functions you pass, calls each one, and then waits for each returned promise settle before continuing on.

import sequential from '@brianmcallister/sequential-promise';

const asyncRequests = [
  () => fetchUser({ id: 1 }),
  (user1) => fetchOrderDetails(user1.orders),
];

sequential(asyncRequests).then(([user1, userOrderDetails]) => {
  renderOrderDetails(userOrderDetails);
});

// ...or with async/await:
const [user1, userOrderDetails] = await sequential(asyncRequests);

renderOrderDetails(userOrderDetails);
⇡ Top

API

Functions

sequential

import sequential from '@brianmcallister/sequential-promise';

sequential is the default export. It iterates over an array of functions that return promises. If one of the promises rejects, then everything will stop, and sequential will return a rejected promise, just like how Promise#all behaves.

sequential: <T>(funcs: ((list: T[]) => Promise<T>)[]) => Promise<T[]>;

All promises resolving:

import sequential from '@brianmcallister/sequential-promise';

const promises = [
  () => Promise.resolve('one'),
  () => Promise.resolve('two'),
];

const results = await sequential(promises);
// #=> ['one', 'two'];

Some promises rejecting:

import sequential from '@brianmcallister/sequential-promise';

const promises = [
  () => Promise.resolve('one'),
  () => Promise.reject('oops'),
];

try {
  await sequential(promises)
} catch (err) {
  console.log(err);
  // #=> 'oops';
}

sequentialAllSettled

import { sequentialAllSettled } from '@brianmcallister/sequential-promise';

sequentialAllSettled attempts to behave the same way the forthcoming `Promise#allSettled behaves.

Even if one of the promises rejects, the iteration won't stop. Instead, the results of every promise are gathered up and the final promise resolves with a summary of all the promises settled values as Result<T>[] (See: Result<T> below.

sequentialAllSettled: <T>(funcs: ((list: Result<T>[]) => Promise<T>)[]) => Promise<Result<T>[]>;

Example:

import { sequentialAllSettled } from '@brianmcallister/sequential-promise';

const promises = [
  () => Promise.resolve('one'),
  () => Promise.reject('oops'),
];

const results = await sequentialAllSettled(promises);
// #=> [{ status: 'fulfilled', value: 'one' }, { status: 'rejected', reason: 'oops' }];
⇡ Top

Types

Result<T>

Settled value when using sequentialAllSettled.

import { Result } from '@brianmcallister/sequential-promise';
interface Fulfilled<T> {
    status: 'fulfilled';
    value: T;
}

interface Rejected {
    status: 'rejected';
    reason: unknown;
}

type Result<T> = Fulfilled<T> | Rejected;
⇡ Top

Keywords

typescript

FAQs

Package last updated on 05 Apr 2020

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

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.