![Oracle Drags Its Feet in the JavaScript Trademark Dispute](https://cdn.sanity.io/images/cgdhsj6q/production/919c3b22c24f93884c548d60cbb338e819ff2435-1024x1024.webp?w=400&fit=max&auto=format)
Security News
Oracle Drags Its Feet in the JavaScript Trademark Dispute
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
@thrall/async-chain
Advanced tools
In many programming languages, the builder pattern is a popular way to write expressive APIs that modify an internal state. For example, instead of writing raw SQL queries or using an ORM, KnexJS allows you to chain together the pieces of the query:
knex('users')
.select('*')
.where('id')
.first();
Typically, patterns like this can be trivially implemented by returning this
from the methods:
class Builder {
method1() {
console.log("Method 1");
return this;
}
method2() {
console.log("Method 2");
return this;
}
method3() {
console.log("Method 3");
return this;
}
}
(new Builder())
.method1()
.method2()
.method3();
Chains are also very common in functional constructs:
[ ... Array(20) ]
.map (x => Math.random())
.sort()
.reduce((x, y) => x + y)
The problem arises when any function in the chain is asynchronous and returns a promise. This will not work:
class Builder {
async method1() {
console.log("Method 1");
return this;
}
async method2() {
console.log("Method 2");
return this;
}
async method3() {
console.log("Method 3");
return this;
}
}
// Throws: Uncaught TypeError: (intermediate value).method1(...).method2 is not a function
await (new Builder())
.method1()
.method2()
.method3();
To make this work you must add additional boilerplate:
await (new Builder())
.method1()
.then((builder) => builder.method2())
.then((builder) => builder.method3())
That's where async-chain
comes in! You can either use the chain
function to wrap an asynchronous object or function or you can extend the Chainable
in your builder.
// Works!
await chain(new Builder())
.method1()
.method2()
.method3();
It is worth noting that while there are many existing libraries that provide methods for chaining asynchronous operations. Most of them arguably add even more boilerplate than just chaining a series of .then
promises.
To install the library, run npm install --save @thrall/async-chain
.
There are two ways to use async-chain
. If you are using ES6 classes, you can extend Chainable
:
import { Chainable } from "async-chain";
class Builder extends Chainable {
// ...
}
or you can wrap an object or function with the chain
method before your await call:
import { chain } from "async-chain";
await chain(object)
.method1()
.method2()
.property
.slice(1) [0].length;
You will notice in the example above that properties of an asynchronous result can also be chained, as well as synchronous methods, native prototypes and array accessors. If at any point in the chain there is an undefined property or an error occurs, the entire result will return false. If strict mode is enabled and an error occurs or a requested property does not exist, the entire chain will throw an error.
Like normal promises, you can use .then
, .catch
, and .finally
:
chain(object)
.method1()
.method2()
.property
.slice(1) [0].length
.then((length) => length / 2)
.then((length) => console.log(length))
.catch((error) => console.error(error))
.finally(() => console.log("Finished"));
You can also use the "pipe" function (when pipe is not already defined as a property on the chain's context) which allows you to operate on the current value of the chain without having to write a separate chain. For example, instead of:
let value = await chain([ "1", "2", "3" ])
.slice(1) [0];
value = +value;
console.log(value); // 2
You can write:
let value = await chain([ "1", "2", "3" ])
.slice(1) [0]
.pipe((value) => +value);
console.log(value); // 2
function chain(target = global, strict = false, finalize = false): Chainable
class Chainable<T> extends Function {
constructor(target?: Object | Function, strict?: boolean, finalize?: boolean): ProxyHandler
}
Node v18 introduces a native implementation of the browser's fetch API:
import { chain } from "async-chain";
const ip = await chain()
.fetch("https://httpbin.org/anything")
.json()
.origin;
This library improves upon the proxy method established in Awaitium.
FAQs
Chain asynchronous methods with await.
We found that @thrall/async-chain demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
Security News
The Linux Foundation is warning open source developers that compliance with global sanctions is mandatory, highlighting legal risks and restrictions on contributions.
Security News
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.