Security News
pnpm 10.0.0 Blocks Lifecycle Scripts by Default
pnpm 10 blocks lifecycle scripts by default to improve security, addressing supply chain attack risks but sparking debate over compatibility and workflow changes.
@antoniovdlc/await-on
Advanced tools
Go-like error handling for async JavaScript functions (and more).
Go-like error handling for async JavaScript functions.
This package is distributed via npm:
npm install @antoniovdlc/await-on
Async functions in JavaScript are great! They allow you to write asynchronous code as if it were synchronous.
The main drawback I personally experience is having to write code like this:
try {
const result = await someFunc()
// Do something with `result`
} catch (err) {
// Handle error
}
Having had some past experience using Go, and after some time to fully understand the elegance of its simplistic error handling approach, it felt right to try to replicate it:
result, err := someFunc();
if err != nil {
// Handle error
}
// Do something with `result`
This is why this package exists, so that we can write asynchronous JavaScript code in a style as close as possible to that of Go:
const [result, err] = await on(someFunc)
if (err != null) {
// Handle error
}
// Do something with `result`
You can use this library either as an ES module or a CommonJS package:
import on from "@antoniovdlc/await-on";
- or -
const on = require("@antoniovdlc/await-on");
The return is formatted as follow:
const [result, error] = await on(someFunc)
In the case where someFunc()
throws an error, the function call will return [null, error]
, else, it will return the computed result
and error
will be null
.
You can pass the following arguments to on()
:
The following examples always populate result
with the value 42
and error
with null
:
const value = 42;
const [result, error] = await on(value);
const promise = new Promise(resolve => resolve(42));
const [result, error] = await on(promise);
const fn = () => 42;
const [result, error] = await on(fn);
The following examples always set result
to null
and error
to an Error
:
const someError = new Error("Some Error");
const [result, error] = await on(someError);
const promise = new Promise((_, reject) => reject("Some Error"));
const [result, error] = await on(someError);
const someError = new Error("Some Error");
const fn = () => {
throw someError;
};
const [result, error] = await on(fn);
As an extra goodie, you can also pass an Array
:
const value = 42;
const promise = new Promise(resolve => resolve(42));
const fn = () => 42;
const [result, error] = await on([promise, fn, value]);
console.log(result); // [42, 42, 42]
You can even go into destructuring overdrive!
const [[r1, r2], error] = await on([promise1, promise2])
Note that the whole call to
on()
will return anerror
if one of the elements of the Array either throws or rejects.
Besides getting inspiration from Go's error handling, this package also draws inspiration from the following packages:
Good you ask!
There is probably a bit of selfishness in the belief that maybe I can implement this in a better way, but the main reason is to see if I can implement such a library and maybe add some goodies on top.
This code, even though thoroughly tested with 100% code coverage, has not ran in production yet. As such, either you feel adventurous, or I would invite you to look for more mature libraries providing essentially the same functionalities (I've linked to a few I found myself right above, but there are many more).
Anyway, thanks for stopping by! :)
Pretty much for the same reason React hooks (and many other libraries) return an Array (most of the time) and not an Object: it gives you the ability to name the destructured variables as you please!
MIT
FAQs
Go-like error handling for async JavaScript functions (and more).
The npm package @antoniovdlc/await-on receives a total of 2 weekly downloads. As such, @antoniovdlc/await-on popularity was classified as not popular.
We found that @antoniovdlc/await-on 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
pnpm 10 blocks lifecycle scripts by default to improve security, addressing supply chain attack risks but sparking debate over compatibility and workflow changes.
Product
Socket now supports uv.lock files to ensure consistent, secure dependency resolution for Python projects and enhance supply chain security.
Research
Security News
Socket researchers have discovered multiple malicious npm packages targeting Solana private keys, abusing Gmail to exfiltrate the data and drain Solana wallets.