Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/elemental-mind/awaitium-js

Put your await on fire and chain together async functions, promises and syncronous code in one line.
https://github.com/elemental-mind/awaitium-js

async async-await chainable-interface chaining promises

Last synced: 6 days ago
JSON representation

Put your await on fire and chain together async functions, promises and syncronous code in one line.

Awesome Lists containing this project

README

        

# Awaitium - chain multiple awaits into one
Put your await on fire and make your code flow without intermediate awaits! Chain asynchrounous (and synchronous) functions together easily.

The best thing: Get full type support along the way. No fighting with the Typescript compiler. It's tamed!

# Example
Write...
````
async function testChainedAwait()
{
....
chainResult = await ing(asyncGetObject()).asyncGetAnotherObject().objectProperty.asyncInstanceGetter().synchronousFunction();
....
}
````
...instead of...
````
async function helpIAmRunningOutOfVariableNames()
{
....
const initialObject = await asyncGetObject();
const intermediateObject = await initialObject.asyncGetAnotherObject();
const wantedInstance = await intermediateObject.objectProperty.asyncInstanceGetter();

chainResult=wantedInstance.synchronousFunction();
....
}
````
# Installation
## Deno
In deno...
````
include { ing } from https://deno.land/x/awaitium@VERSION/source/awaitium.ts
````
## Browser & Node
> Both these environments are untested. Raise an issue if you come across any problems.

If you use npm do...
````
npm i awaitium
````
then in your sources...
````
include { ing } from "awaitium"
````

# Usage
To use Awaitium, simply wrap the entry point of your chain with the `ing()` function. An entry point can be one of the following:
- a function call to a synchronous function:
````
await ing(syncFunction()).followedByAnyOf.asyncFunc().syncFunc().orPropOrMember
````
- a function call to an asynchronous function:
````
await ing(asyncFunction()).followedByAnyOf.asyncFunc().syncFunc().orPropOrMember
````
- any object:
````
await ing(object).followedByAnyOf.asyncFunc().syncFunc().orPropOrMember
````

The chain following the `ing` call may comprise any combination of the following:
- async function calls
- synchronous function calls
- property reads
- member reads

# Good to know
You must start your call chain (the start of the chain is the closing bracket of the `ing` function call) after the first call to an async function - but you can start your chain already at the very beginning:
````
await ing(object.member.asyncFunction()).syncFunction().doSomethingElseAsync()
````
... is the same as ...
````
await ing(object).member.asyncFunction().syncFunction().doSomethingElseAsync()
````
I like it clean: Personally I prefer wrapping objects (or function calls) the earliest possible.

# Caveats
Behind the scenes every function call is awaited. As you can also chain synchronous function calls, that means these synchronous calls may also be waiting for their turn of the event loop (depending on the JS engine/environment, I guess?!).
This is uninvestigated, however - if you'd like to add more insight into this, feel free to raise an issue. In most applications this should have negligable impact, though.

# License
MIT