Skip to content

Repository files navigation

A small library for working with async data

Description

the library provides only one function that you can use to wrap your async data

the function uses proxy object that stores your changes and apply them by the time your data is arrived

Example

one of the ways of using async is to pass a function that returns promise-like object with your data

import{async}from'async';interfaceData{foo: {bar: string[];}}functionrequest(): Promise<Data>{// ...}constfirstUpperCaseString=awaitasync(request).foo.bar.at(0)?.toUpperCase();

by the time your promise-like is resolved the data will have the first element from the array of strings transformed to upper case

the async function returns a simple promise so you can call Promise.prototype methods

async(request).foo.bar.then(()=>{}).catch(()=>{}).finally(()=>{});

you can also create multiple independent promises

constfoo=async(request).foo,bar=foo.bar,firstUpperCase=bar.at(0)?.toUpperCase();

if you want to unwrap all of them you can use Promise.all or any other static method

awaitPromise.all([foo,bar,firstUpperCase]);

you can also pass a promise-like object or just simple data and it's gonna be wrapped in promise

async(Promise.resolve({foo: 21})).foo;async({then(cb){cb({bar: 21})}}).bar;async({baz: 21}).baz;

the async function also will be useful when you have some functions and each of them returns a promise

constend={end(){returnPromise.resolve(21);}}constprocess={process(){returnPromise.resolve(end);}}conststart={start(){returnPromise.resolve(process);}}

imagine you want to get 21

you would write something like this

constprocess=awaitstart.start(),end=awaitprocess.process(),twentyOne=awaitend.end();

with async you can do the same thing this way

consttwentyOne=awaitasync(start).start().process().end();

API

functionasync<Data>(data: Data): Promisify<Data>;functionasync<Data>(promise: Promise<Data>): Promisify<Data>;functionasync<Data>(getPromise: ()=>Promise<Data>): Promisify<Data>;

the Promisify type simply patches all of your fields and methods so they will return a promise

About

A small library for working with async data

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages