Promist
A dependable promises and async utility belt.
Install
npm install promist
Basics
Promist intends to cover and abstract the most common day to day dealings with async behavior and promises. It doesn't intend to be the most complete library, or an incredibly slim one, but rather be a dependable set of functions that serve as go-to for most use cases.
- Create functions: return a new promise.
- Classes: both
Promist
and LazyPromist
behave just like a Promise
, but pack a few additional features.
- Utils: a set of conveniency utility functions.
- Collections: handled either in parallel or series.
Create functions
Create functions return a new promise.
wait(ms: number): Promise
Returns a promise that will resolve after ms
milliseconds;
ms
: number of milliseconds to wait for until resolution.
import { wait } from 'promist';
wait(100).then(() => console.log('Resolved after 100ms'));
until(test: Function, safe?: boolean, ms?: number): Promise
Returns a promise that resolves when test
returns true
.
test
: test function, with signature () => boolean | Promise<boolean>
.safe
: if true
, it will treat test
throws and rejections as false
, instead of rejecting itself.ms
: the frequency test
should be called at until it returns truthy. Default: 25
.
import { until } from 'promist';
let example = 1;
until(() => example === 10)
.then(() => console.log('Resolved after example = 10'));
example = 10;
subscribe(observable: Observable, onComplete?: Function): Promise
Subscribes to an observable
and resolves/rejects with its first value. By default, it will reject if the observable completes before emitting any values, though this behavior can be controlled via onComplete
.
observable
: an Observable object.onComplete
: a promise executor function, handling the event of the observable completing without emitting any values, and with signature: (resolve: Function, reject: Function): void
.
import { subscribe } from 'promist';
import { Subject } from 'rxjs';
const subject = new Subject();
subscribe(subject)
.then(console.log);
subject.next('foo');
Classes
Promist
class
Promist
behaves just like a Promise
, but packs a few additional features.
- It can be externally resolved and/or rejected.
- It can also be externally cancelled. If using an executor on the
Promist
constructor, you can receive external completion events (resolution/rejection/cancellation) via the returned callback in order to free up resources, if needed. Externally, you also have access to this event -including cancellation- via the Promist.react
promise. - It will always have the
finally
method available, regardless of the underlying Promise
implementation.
The difference between Promist
s static methods and create functions is that in any completion event, they will always clean up after themselves, clearing the underlying timeouts and/or subscriptions.
Its constructor takes an optional executor function, just as
you would use to instantiate a normal promise.
import { Promist } from 'promist';
const promist = new Promist((resolve, reject) => {
const timeout = setTimeout(() => resolve('foo'), 250);
return function cleanup() {
clearTimeout(timeout);
}
});
LazyPromist
class
Inherits from Promist
, having the same constructor signature, with the diference of the executor not being optional. All of its static methods promises will execute lazily, as expected.
LazyPromist
s don't run their constructor executor
until after they've been explicitly expected to resolve by a then
, catch
, or finally
call.
import { LazyPromist } from 'promist';
const promist = new LazyPromist((resolve, reject) => {
const timeout = setTimeout(() => resolve('foo'), 250);
return function cleanup() {
clearTimeout(timeout);
}
});
Static methods
Promist.from(promise: Promise | Function): Promist
Creates a Promist
from a Promise
or a sync or async function.
promise
: a Promise
or a function.
import { Promist } from 'promist';
Promist.from(Promise.resolve('foo'));
Promist.from(async () => 'bar');
Promist.from(() => 'baz');
Promist.wait(ms: number): Promist
See wait
and the differences between Promist
static methods and create functions.
Promist.until(test: Function, safe?: boolean, ms?: number): Promist
See until
and the differences between Promist
static methods and create functions.
Promist.subscribe(observable: Observable, onComplete?: Function): Promist
See subscribe
and the differences between Promist
static methods and create functions.
Instance fields
promist.status
Any of 'pending'
, 'resolved'
, 'rejected'
and 'cancelled'
.
promist.value
The value the promise has resolved to, if any. Otherwise null
.
promist.reason
The reason the promise has rejected with, if any. Otherwise null
.
promist.react
An empty promise that resolves once the promise has resolved, rejected, or has gotten cancelled.
Instance methods
promist.resolve(value?: any): void
Resolves the Promist
with value
.
value
: the value to resolve to.
import { Promist } from 'promist';
const promist = new Promist();
promist.then(console.log);
promist.resolve('foo');
promist.reject(reason: Error): void
Rejects the Promist
with reason
.
reason
: the Error to reject with.
import { Promist } from 'promist';
const promist = new Promist();
promist.catch(console.error);
promist.reject(Error('foo'));
promist.cancel(): void
Cancels the Promist
. If it didn't already, it will never resolve nor reject.
import { Promist } from 'promist';
const promist = new Promist((resolve) => {
const timeout = setTimeout(resolve, 150);
return () => clearTimeout(timeout);
});
promist.cancel();
promist.then(console.log);
promist.timeout(ms: number, reason?: Error): void
Sets a timeout of ms
milliseconds after which, if the Promist
hasn't resolved, rejected, or cancelled, it will reject with reason
, or a default error if no reason
is passed.
ms
: timeout in milliseconds.reason
: Error to reject with.
import { Promist } from 'promist';
const promist = new Promist((resolve) => {
const timeout = setTimeout(resolve, 150);
return () => clearTimeout(timeout);
});
promist.timeout(50);
promist.catch(console.error);
promist.fallback(ms: number, value: any): void
Sets a timeout of ms
milliseconds after which, if the Promist
hasn't resolved, rejected, or cancelled, it will resolve by falling back to value
.
ms
: timeout in milliseconds.value
: value to resolve with.
import { Promist } from 'promist';
const promist = new Promist((resolve) => {
const timeout = setTimeout(() => resolve('foo'), 150);
return () => clearTimeout(timeout);
});
promist.fallback(50, 'bar');
promist.then(console.log);
Utils
control(test: Function, generator: Function): Function
Used to control async flow. It returns a promise returning function taking the same arguments as generator
.
test
: a test function (can be async
) that will be run before calling each next()
on generator
, with signature () => Promise<boolean | Error> | boolean | Error
. It can return:
false
: generator
will not continue execution (it will never resolve).true
: generator
will continue execution until the next yield
.Error
: generator
call will return a rejected promise. The same behavior can be expected if the error is thrown instead of returned.
generator
: must be a generator function. Within it, you'd use yield
as you would await
for an async
function.
import { control } from 'promist';
function* gen(n) {
let res = yield Promise.resolve(n * 2);
res = yield Promise.resolve(res * 5);
return res;
}
const willContinue = control(() => true, gen);
const willNotContinue = control(() => false, gen);
const willReject = control(() => Error('An error ocurred'), gen);
willContinue(1).then(console.log);
willNotContinue(2).then(console.log);
willReject(3).then(console.log).catch(console.error);
isPromise(value: any): boolean
Returns true
if value
is a thenable, false
otherwise.
import { isPromise } from 'promist';
if (isPromise(promise)) {
promise.then(() => { });
}
Collections
-
Series:
- Collection functions execute serially.
- The passed functions (callbacks) receive an array of promises.
-
Parallel:
- Collection functions execute in parallel in two stages: first, the resolution of all promises, then the passed function calls.
- The passed functions (callbacks) receive an array with the values the input array of promises resolved to.
parallel.reduce()
receives a promise as the accumulator parameter.
import { parallel } from 'promist';
import { series } from 'promist';
parallel.map(promiseArr, (x, i, arr) => {
return x;
});
series.map(promiseArr, (x, i, arr) => {
return x;
})
map(arr: Promise[], callback: Function): Promise
arr
: An array of promises.callback
: With the same signature as Array.prototype.map()
. Can be a promise returning/async function.
filter(arr: Promise[], callback: Function): Promise
arr
: An array of promises.callback
: With the same signature as Array.prototype.filter()
. Can be a promise returning/async function.
reduce(arr: Promise[], callback: Function, initialValue: any): Promise
arr
: An array of promises.callback
: With the same signature as Array.prototype.reduce()
. Can be a promise returning/async function.initialValue
: An initial value; if absent, the resolved value of the first promise in the array will be taken as initialValue
.
each(arr: Promise[], callback: Function): Promise
arr
: An array of promises.callback
: With the same signature as Array.prototype.forEach()
. Can be a promise returning/async function.