{"id":24942,"library":"await-to-js","title":"await-to-js","description":"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.","status":"active","version":"3.0.0","language":"javascript","source_language":"en","source_url":"https://github.com/scopsy/await-to-js","tags":["javascript","node","async","await","async await","typescript"],"install":[{"cmd":"npm install await-to-js","lang":"bash","label":"npm"},{"cmd":"yarn add await-to-js","lang":"bash","label":"yarn"},{"cmd":"pnpm add await-to-js","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"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.","wrong":"const to = require('await-to-js')","symbol":"to","correct":"import to from 'await-to-js'"},{"note":"The package uses default export, not named export. Using named import will give undefined.","wrong":"import { to } from 'await-to-js'","symbol":"to (TypeScript)","correct":"import to from 'await-to-js'"},{"note":"In TypeScript, the generic parameter specifies the resolved type. CommonJS users should still use .default to get the function.","wrong":"const to = require('await-to-js').default; const [err, data] = await to<{test: number}>(promise);","symbol":"to with generic type","correct":"import to from 'await-to-js'; const [err, data] = await to<{test: number}>(promise);"}],"quickstart":{"code":"import to from 'await-to-js';\n\nasync function fetchData() {\n  const promise = Promise.resolve({ id: 1, name: 'test' });\n  const [err, data] = await to(promise);\n  if (err) {\n    console.error('Error:', err);\n    return;\n  }\n  console.log('Data:', data);\n}\n\nfetchData();","lang":"typescript","description":"Demonstrates basic usage of await-to-js to handle a resolved promise, checking for error first."},"warnings":[{"fix":"Update comparisons: if (err !== null) should become if (err !== undefined).","message":"In version 2.0.0, the return type changed from [Error|null, any] to [Error|undefined, any]. Check for existing code that strictly compares null.","severity":"breaking","affected_versions":">=2.0.0 <3.0.0"},{"fix":"Use import to from 'await-to-js' or const to = require('await-to-js').default;","message":"In version 3.0.0, the package switched to ESM-only. CommonJS require() will not work without .default.","severity":"breaking","affected_versions":">=3.0.0"},{"fix":"Ensure your promises reject with Error objects for consistent error handling.","message":"If the promise rejects with a non-Error value (e.g., a string), the error component will be that value, not an Error instance.","severity":"gotcha","affected_versions":"all"},{"fix":"Upgrade to latest version and use default import. For old code, use import { to } from 'await-to-js'.","message":"Version 1.x exported a named export instead of default. Using import to from 'await-to-js' would fail.","severity":"breaking","affected_versions":"<2.0.0"}],"env_vars":null,"last_verified":"2026-05-01T00:00:00.000Z","next_check":"2026-07-30T00:00:00.000Z","problems":[{"fix":"Use const to = require('await-to-js').default;","cause":"Using CommonJS require without .default: const to = require('await-to-js') returns an object with a default property.","error":"TypeError: to is not a function"},{"fix":"Change import { to } from 'await-to-js' to import to from 'await-to-js'","cause":"Trying to import a named export when the package only provides a default export.","error":"Module '\"await-to-js\"' has no exported member 'to'."},{"fix":"Install the package: npm install await-to-js. Ensure tsconfig.json includes 'node' in moduleResolution or set 'esModuleInterop': true.","cause":"TypeScript resolution issue; package may not be installed or tsconfig misconfigured.","error":"Cannot find module 'await-to-js' or its corresponding type declarations."}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}