Skip to main content
Module

x/fun/promise.ts

A collection of algebraic data types, lenses, and schemables based on a light weight higher kinded type implementation. Written for deno.
Latest
import * as fun from "https://deno.land/x/fun@v2.0.0/promise.ts";

This file contains the Promise algebraic data type. Promise is the javascript built in data structure for asynchronous computation.

Variables

The canonical implementation of Applicable for Promise. It contains the methods wrap, apply, and map.

The canonical implementation of Flatmappable for Promise. It contains the methods wrap, apply, map, and flatmap.

The canonical implementation of Mappable for Promise. It contains the method map.

Functions

Make an existing Promise somewhat abortable. While the returned promise does resolve when the abort signal occurs, the existing promise continues running in the background. For this reason it is important to catch any errors associated with the original promise and to not implement side effects in the aborted promise.

An alias for Promise.all

Create a new Promise from a Promise<(a: A) => I> and a Promise. Although Promises encapsulate asynchrony, there is no way defer a Promise once created, thus this ap function always evaluates both input Promises in parallel.

An alias for Promise.catch

Create a Deferred from a type.

Delay the resolution of an existing Promise. This does not affect the original promise directly, it only waits for a ms milliseconds before flatmaping into the original promise.

Create a new Promise by flatmaping over the result of an existing Promise. This is effectively Promise.then.

Create a new Promise by mapping over the result of an existing Promise. This is effectively Promise.then, but narrowed to non-promise returning functions. If the mapping function returns a Promise then the type for this function will be incorrect, as there is no way to create a Promise<Promise>.

An alias for Promise.race. Note that Promise.race leaks async operations in most runtimes. This means that the slower promise does not stop when the faster promise resolves/rejects. In effect Promise.race does not handle cancellation.

An alias for Promise.reject.

An alias for Promise.resolve.

An alias for Promise.then

Wrap a function that potentially throws in a try/catch block, handling any thrown errors and returning the result inside of a Promise.

Create a Promise that resolve after ms milliseconds.

Create a Promise from a value A or another PromiseLike. This is essentially an alias of Promise.resolve.

Interfaces

Specifies Promise as a Higher Kinded Type, with covariant parameter A corresponding to the 0th index of any Substitutions.

Type Aliases

A type for Promise over any, useful as an extension target for functions that take any Promise and do not need to unwrap the type.

A Promise with the inner resolve function hoisted and attached to itself.

A type level unwrapor, used to pull the inner type from a Promise.