Registry / http-networking / run-async

run-async

JSON →
library4.0.6jsnpmunverified

run-async is a utility method designed to normalize the execution of functions that can operate synchronously, asynchronously via a `this.async()` callback, or by returning a Promise. This library is particularly useful for authors of middleware or plugins that need to accept user-provided functions with varying asynchronous patterns, ensuring a consistent execution flow. The current stable version is 4.0.6, and the project demonstrates an active maintenance cadence, addressing dependency issues and enhancing functionality across major versions. A key differentiator is its ability to abstract away the underlying async mechanism, providing a single interface, and its current status as a dependency-free package since version 2.4.1, which improves reliability and reduces supply chain risks.

npm install run-async
INSTALL
IMPORT
SIG · RUN-ASYNC
R
run-async
http-networkingjavascriptv4.0.6
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

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

runAsync
import { runAsync } from 'run-async';
const runAsync = require('run-async');
While CommonJS `require` is shown in the README, modern Node.js and TypeScript projects should use ESM imports.
runAsync.cb
import { runAsync } from 'run-async'; runAsync.cb(...);
import { cb } from 'run-async';
`cb` is a property of the main `runAsync` export, not a separate named export.
RunAsync function types
import type { RunAsync } from 'run-async';
For type-checking in TypeScript, import the `RunAsync` interface or similar utility types.

Demonstrates how to use `runAsync` to execute functions that use `this.async()`, return a Promise, or return a synchronous value. Also includes an example of `runAsync.cb` for Node.js callback-style functions.

import { runAsync } from 'run-async'; // A helper to demonstrate how runAsync normalizes different function types const printAfter = async (func: Function) => { const cb = (err: Error | null, returnValue: unknown) => { if (err) { console.error('Error:', err); return; } console.log(returnValue); }; // Execute the function using runAsync, passing a callback // runAsync returns a function that takes arguments for func try { const result = await runAsync(func, cb)(); // If func returns a Promise, runAsync resolves it. The callback also fires. // We'll catch the potential Promise resolution here. // The callback provided to runAsync will still be called. } catch (error) { console.error('Caught an error from runAsync execution:', error); } }; console.log('--- Using this.async ---'); printAfter(function (this: any) { const done = this.async(); setTimeout(() => { done(null, 'done running with callback'); }, 10); }); console.log('\n--- Returning a Promise ---'); printAfter(function () { return new Promise(resolve => { setTimeout(() => resolve('done running with promises'), 5); }); }); console.log('\n--- Synchronous function ---'); printAfter(function () { return 'done running sync function'; }); // Example using runAsync.cb for Node.js callback style console.log('\n--- Using runAsync.cb (Node.js callback style) ---'); runAsync.cb( (a: number, b: number, cb: (err: Error | null, result: number) => void) => { setTimeout(() => cb(null, a + b), 20); }, (err: Error | null, result: number) => { if (err) console.error('Error with runAsync.cb:', err); else console.log(`runAsync.cb result: ${result}`); } )(5, 7);
Debug
Known issues
breakingVersion 2.0.0 changed the `runAsync` signature. If your Node.js version supports native Promises, `runAsync` will now return a Promise, altering how it's consumed compared to earlier versions that exclusively used callbacks.
fix
Migrate your usage to consume the returned Promise (e.g., `runAsync(func)(args).then(cb)`) or ensure your callback handling is compatible with both Promise-based and traditional callback flows.
affects: >=2.0.0
breakingThe `runAsync.cb` function, introduced in v2.1.0, requires the wrapped function to have a fixed number of parameters. Variable argument functions or functions with `arguments` objects may not behave as expected.
fix
Ensure that any function passed to `runAsync.cb` explicitly defines all its parameters, including the final callback argument.
affects: >=2.1.0
gotchaPrior to v2.2.0, Promise polyfill behavior was conditional. Since v2.2.0, `runAsync` consistently returns a Promise, relying on the 'Pinkie' polyfill if native Promises are not available in the Node.js environment.
fix
Be aware that `runAsync` will always return a Promise for consistent behavior across Node.js versions, which might slightly change execution flow compared to pre-2.2.0 versions on older Node.js.
affects: >=2.2.0
gotchaThe `is-promise` dependency was removed in v2.4.1 due to issues with the upstream package, making `run-async` dependency-free. Older versions might have experienced unexpected behavior or issues related to this dependency.
fix
Upgrade `run-async` to version 2.4.1 or higher to remove the `is-promise` dependency and ensure a more stable, dependency-free experience.
affects: <2.4.1
gotchaThe Node.js 0.10 Promise polyfill was removed in v2.3.0. Users on very old Node.js 0.10 environments might experience Promise-related issues without the polyfill.
fix
Upgrade your Node.js runtime to a supported version (e.g., 0.12 or higher as per package.json engines) to ensure native Promise support or proper polyfill handling by `run-async`.
affects: >=2.3.0
Errors
Common errors & fixes
TypeError: this.async is not a function
The function passed to `runAsync` is not being called with the correct `this` context, which typically provides the `async` method.
fix
Ensure `runAsync` is called such that the wrapped function's `this` context is correctly bound or provided by `run-async` itself. Avoid using arrow functions for the wrapped function if you intend to use `this.async()`, as arrow functions lexically bind `this`.
Error: Callback was already called.
This error typically occurs when the `done()` callback (from `this.async()`) or a Promise's `resolve`/`reject` function is invoked more than once within the wrapped function.
fix
Review your asynchronous logic to ensure that `done()`, `resolve()`, or `reject()` are called exactly once per `runAsync` invocation, even in error scenarios. Add guards to prevent multiple calls.
Upgrade
Version history
4.0.6latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
7 hits · last 30 days
node
6
OpenAI (training)
1
Resources
run-async — npm install run-async · libregistry