Registry / http-networking / await-to-js

await-to-js

JSON →
library3.0.0jsnpmunverified

await-to-js is a lightweight zero-dependency utility that wraps a Promise and returns an array with either an error or the resolved value, enabling a Go-like error handling pattern in async/await functions. The current stable version is 3.0.0, with a stable release cadence. It ships TypeScript definitions. Differentiators: extremely simple API, tree-shakable, no external dependencies. It is commonly used in Node.js and browser environments to avoid try-catch blocks and improve code readability when handling promise rejections.

npm install await-to-js
INSTALL
IMPORT
SIG · AWAIT-TO-JS
A
await-to-js
http-networkingjavascriptv3.0.0
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

to
import to from 'await-to-js'
const to = require('await-to-js')
In CommonJS, you must use require('await-to-js').default because the package exports a default export. Using require without .default will result in an object with a default property.
to (TypeScript)
import to from 'await-to-js'
import { to } from 'await-to-js'
The package uses default export, not named export. Using named import will give undefined.
to with generic type
import to from 'await-to-js'; const [err, data] = await to<{test: number}>(promise);
const to = require('await-to-js').default; const [err, data] = await to<{test: number}>(promise);
In TypeScript, the generic parameter specifies the resolved type. CommonJS users should still use .default to get the function.

Demonstrates basic usage of await-to-js to handle a resolved promise, checking for error first.

import to from 'await-to-js'; async function fetchData() { const promise = Promise.resolve({ id: 1, name: 'test' }); const [err, data] = await to(promise); if (err) { console.error('Error:', err); return; } console.log('Data:', data); } fetchData();
Debug
Known issues
breakingIn version 2.0.0, the return type changed from [Error|null, any] to [Error|undefined, any]. Check for existing code that strictly compares null.
fix
Update comparisons: if (err !== null) should become if (err !== undefined).
affects: >=2.0.0 <3.0.0
breakingIn version 3.0.0, the package switched to ESM-only. CommonJS require() will not work without .default.
fix
Use import to from 'await-to-js' or const to = require('await-to-js').default;
affects: >=3.0.0
gotchaIf the promise rejects with a non-Error value (e.g., a string), the error component will be that value, not an Error instance.
fix
Ensure your promises reject with Error objects for consistent error handling.
affects: all
breakingVersion 1.x exported a named export instead of default. Using import to from 'await-to-js' would fail.
fix
Upgrade to latest version and use default import. For old code, use import { to } from 'await-to-js'.
affects: <2.0.0
Errors
Common errors & fixes
TypeError: to is not a function
Using CommonJS require without .default: const to = require('await-to-js') returns an object with a default property.
fix
Use const to = require('await-to-js').default;
Module '"await-to-js"' has no exported member 'to'.
Trying to import a named export when the package only provides a default export.
fix
Change import { to } from 'await-to-js' to import to from 'await-to-js'
Cannot find module 'await-to-js' or its corresponding type declarations.
TypeScript resolution issue; package may not be installed or tsconfig misconfigured.
fix
Install the package: npm install await-to-js. Ensure tsconfig.json includes 'node' in moduleResolution or set 'esModuleInterop': true.
Upgrade
Version history
3.0.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
13 hits · last 30 days
node
12
OpenAI (training)
1
Resources
await-to-js — npm install await-to-js · libregistry