npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@ze-ts/promise

v0.0.0

Published

A set of functions work with promises in a functional way.

Downloads

5

Readme

@ze-ts/promise

A collection of utility functions to work with Promises.

Installation

npm install @ze-ts/promise

Usage

Note

ZTSPromise

ZTSPromise is a type alias for a Promise that can resolve to a value of type T or reject with an error of type E. This is useful to work with the two sides of a Promise.

type ZTSPromise<T, E> = Promise<T>;

then

A curried function that takes a function and a Promise and returns a new Promise that will resolve to the result of applying the function to the resolved value of the original Promise. For legibility and semantic reasons, the type of this function enforces that the function passed to it returns a ZTSPromise. If you want to manipulate the value directly, use the map function instead.

Signature

then: <A, B, E>(fn: (value: A) => ZTSPromise<B, E>) =>
  (promise: ZTSPromise<A, E>) =>
    ZTSPromise<B, E>;

Example

Usage with pipe
import { then } from '@ze-ts/promise';
import { pipe } from '@ze-ts/composition';

const add = (a: number) => (b: number) => Promise.resolve(a + b);
const multiply = (a: number) => (b: number) => Promise.resolve(a * b);

const add3 = add(3); // (b: number) => Promise<number>
const multiply2 = multiply(2); // (b: number) => Promise<number>
const promise = Promise.resolve(2); // Promise<2>

const result = await pipe(promise, then(add3), then(multiply2)); // Promise<10>

map

A curried function that takes a function and a Promise and returns a new Promise that will resolve to the result of applying the function to the resolved value of the original Promise. This function is useful when you want to manipulate the value directly, without wrapping it in a Promise. As mentioned in the then function, the type of the function passed to it enforces that the function returns a value of type O. If you want to return a Promise, use the then function instead.

Signature

map: <I, O, E>(fn: (value: I) => O) =>
  (promise: ZTSPromise<I, E>) =>
    ZTSPromise<O, E>;

Example

Usage with pipe
import { map } from '@ze-ts/promise';
import { pipe } from '@ze-ts/composition';

const add = (a: number) => (b: number) => a + b;
const multiply = (a: number) => (b: number) => a * b;

const add3 = add(3); // (b: number) => number
const multiply2 = multiply(2); // (b: number) => number
const promise = Promise.resolve(2); // Promise<2>

const result = pipe(promise, map(add3), map(multiply2)); // Promise<10>

otherwise

A curried function that takes a function and a Promise and returns a new Promise that will resolve to the result of applying the function to the rejected value of the original Promise. This function is useful when you want to handle errors in a Promise chain.

Signature

type Otherwise = <I, E>(
  fn: (e: E) => ZTSPromise<I, E> | I
) => (promise: ZTSPromise<I, E>) => ZTSPromise<I, E>;

Example

Usage with pipe
import { otherwise } from '@ze-ts/promise';
import { pipe } from '@ze-ts/composition';

const add = (a: number) => (b: number) => a + b;
const multiply = (a: number) => (b: number) => a * b;

const add3 = add(3); // (b: number) => number
const multiply2 = multiply(2); // (b: number) => number
const promise = Promise.reject('error'); // Promise<error>

const result = pipe(
  promise,
  otherwise(error => Promise.resolve(0))
); // Promise<0>

tryCatch

A curried function that takes two functions and a Promise and returns a new Promise that will resolve to the result of applying the first function to the resolved value of the original Promise or the result of applying the second function to the rejected value of the original Promise. This function is useful when you want to handle errors in a Promise chain.

Signature

type TryCatch = <I, O, E>(
  trier: (value: I) => O | ZTSPromise<O>,
  onError: (error: E) => O | ZTSPromise<O>
) => (promise: ZTSPromise<I, E>) => ZTSPromise<O, E>;

Example

Usage with pipe
import { otherwise } from '@ze-ts/promise';
import { pipe } from '@ze-ts/composition';

const add = (a: number) => (b: number) => a + b;
const multiply = (a: number) => (b: number) => a * b;

const add3 = add(3); // (b: number) => number
const multiply2 = multiply(2); // (b: number) => number

const promise = Promise.reject('error'); // Promise<error>

const result = pipe(
  promise,
  otherwise(
    () => Promise.resolve(0),
    error => Promise.resolve(error)
  )
); // Promise<0>