async-later 🏄♂️
Return a Promise
or an AsyncIterable
now and handle the logic later! Use in conjunction with the amazing streaming-iterables
package and write elegant functional code.
Install
There are no dependencies.
npm i --save async-later
We ship esm and types.
API
resolveLater
function resolveLater<T>(): [Promise<T>, Resolve<T>]
Creates a Promise
and passes its resolve
to the outer scope (in the native Promise API, resolve
is only accessible through new Promise((resolve, reject) => {...})
).
import { resolveLater } from 'async-later';
const [value, setValue] = resolveLater();
value.then(console.log);
setValue(42);
Real world example adapted from @functionland/protocols/file:
type SaveMethod = (blog: Blog, declareId: (id: string) => void) => any;
let saveMethod: SaveMethod = async () => {};
export function changeSaveMethod(method: SaveMethod) {
saveMethod = method;
}
export function save(blog: Blog): Promise<string> {
const [id, resolve] = resolveLater<string>();
saveMethod(blog, resolve);
return id;
}
iterateLater
function iterateLater<T>(): [AsyncIterable<T>, Resolve<T>, () => void]
Creates next()
and complete()
interfaces for an AsyncIterable
(similar to Observable
s). Stalls on back pressure and caches when read is slower.
import { iterateLater } from 'async-later';
const [iterable, next, complete] = iterateLater();
next(1);
next(2);
next(3);
complete();
for await (const value of iterable) {
console.log(value);
}
partition
function partition<T>(index: number, iterable: AsyncIterable<T>): [AsyncIterable<T>, AsyncIterable<T>]
Decomposes an AsyncIterable
into two at an index
(more partitions can be made by subsequent/recursive calls).
import { partition, toAsyncIterable } from 'async-later';
const [p1, rest] = partition(2, toAsyncIterable([1, 2, 3, 4, 5]));
const [p2, p3] = partition(2, rest);
for await (const value of p1) {
console.log(value);
}
for await (const value of p2) {
console.log(value);
}
for await (const value of p3) {
console.log(value);
}
toAsyncIterable
function toAsyncIterable<T>(
value: T | PromiseLike<T> | ObservableLike<T> | Iterable<PromiseLike<T> | T> | AsyncIterable<T>
): AsyncIterable<T>
export function toAsyncIterable<T>(): (
value: T | PromiseLike<T> | ObservableLike<T> | Iterable<PromiseLike<T> | T> | AsyncIterable<T>
) => AsyncIterable<T>
Converts anything to an AsyncIterable
!
import { toAsyncIterable } from 'async-later';
for await (const value of toAsyncIterable(42)) {
console.log(value);
}
for await (const value of toAsyncIterable(Promise.resolve(42))) {
console.log(value);
}
for await (const value of toAsyncIterable([42])) {
console.log(value);
}
for await (const value of toAsyncIterable([])) {
console.log(value);
}
for await (const value of toAsyncIterable([1, 2, 3])) {
console.log(value);
}
for await (const value of toAsyncIterable([1, Promise.resolve(2), Promise.resolve(3)])) {
console.log(value);
}
firstValue
function firstValue<T>(iterable: Iterable<T> | AsyncIterable<T>): Promise<T>
function firstValue<T>(): (iterable: Iterable<T> | AsyncIterable<T>) => Promise<T>
Returns the first value from an AsyncIterable
as a Promise
. The Promise
rejects if iterable is empty.
import { firstValue, toAsyncIterable } from 'async-later';
const iterable = toAsyncIterable([1, 2, 3]);
console.log(await firstValue(iterable));
lastValue
function lastValue<T>(iterable: Iterable<T> | AsyncIterable<T>): Promise<T>
function lastValue<T>(): (iterable: Iterable<T> | AsyncIterable<T>) => Promise<T>
Returns the last value from an AsyncIterable
as a Promise
. The Promise
rejects if iterable is empty.
import { lastValue, toAsyncIterable } from 'async-later';
const iterable = toAsyncIterable([1, 2, 3]);
console.log(await lastValue(iterable));
valueAt
function valueAt<T>(index: number, iterable: Iterable<T> | AsyncIterable<T>): Promise<T>
function valueAt<T>(index: number): (iterable: Iterable<T> | AsyncIterable<T>) => T
Returns the value specified by an index
in an AsyncIterable
, as a Promise
. The Promise
rejects if iterable is empty or index
>= length.
import { valueAt, toAsyncIterable } from 'async-later';
const iterable = toAsyncIterable([1, 2, 3]);
console.log(await valueAt(1, iterable));
concurrently
function concurrently<T>(...functions: (() => T | PromiseLike<T>)[]): Promise<T[]>
Invokes functions
with Promise.all
.
import { concurrently } from 'async-later';
const result = await concurrently(
() => 42,
() => Promise.resolve(42),
async () => 42,
() => 24,
async () => 24
);
console.log(result)
License
MIT