# run-async

> Utility method to run function either synchronously or asynchronously using the common `this.async()` style.

Latest version **4.0.6** (published 2025-08-08) · MIT license · 0 weekly downloads

## Install

```sh
npm install run-async
pnpm add run-async
yarn add run-async
bun add run-async
```

## Health

**Score 40/100 (D)** — status: maintenance-mode.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: stale; low maintenance score.

## Facts

| | |
|---|---|
| Version | 4.0.6 |
| Published | 2025-08-08 |
| First published | 2014-08-11 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Node | >=0.12.0 |
| Dependencies | 0 |
| Unpacked size | 7.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 24 |
| Author | Simon Boudrias |
| Maintainers | sboudrias |
| Keywords | flow, flow-control, async |

## Links

- npm: https://www.npmjs.com/package/run-async
- Repository: https://github.com/SBoudrias/run-async
- Homepage: https://github.com/SBoudrias/run-async#readme
- Issues: https://github.com/SBoudrias/run-async/issues
- npm.io page: https://npm.io/package/run-async

## Alternatives

- [@commercetools/sync-actions](https://npm.io/package/@commercetools/sync-actions.md) — 25.1K weekly downloads
- [cwait](https://npm.io/package/cwait.md) — 21.4K weekly downloads
- [@ledgerhq/hw-app-cosmos](https://npm.io/package/@ledgerhq/hw-app-cosmos.md) — 4.2K weekly downloads
- [@financial-times/o-loading](https://npm.io/package/@financial-times/o-loading.md) — 2.8K weekly downloads
- [fa](https://npm.io/package/fa.md) — 185 weekly downloads

## Recent versions

- 4.0.6 (latest) — 2025-08-08
- 4.0.5 — 2025-07-21
- 4.0.4 — 2025-06-19
- 4.0.3 — 2025-06-19
- 4.0.2 — 2025-06-19
- 4.0.1 — 2025-06-19
- 4.0.0 — 2025-06-19
- 3.0.0 — 2023-05-07
- 2.4.1 — 2020-04-27
- 2.4.0 — 2020-02-26
- 2.3.0 — 2016-12-02
- 2.2.0 — 2016-03-17
- 2.1.0 — 2016-01-14
- 2.0.0 — 2015-10-18
- 1.0.0 — 2015-10-13
- … 1 more at https://npm.io/package/run-async/versions

## README

# Run Async

[![npm](https://badge.fury.io/js/run-async.svg)](http://badge.fury.io/js/run-async)

Utility method to run a function either synchronously or asynchronously using a series of common patterns. This is useful for library author accepting sync or async functions as parameter. `runAsync` will always run them as an async method, and normalize the multiple signature.

# Installation

```bash
npm install --save run-async
```

# Usage

Here's a simple example print the function results and three options a user can provide a function.

```js
var runAsync = require("run-async");

var printAfter = function (func) {
  var cb = function (err, returnValue) {
    console.log(returnValue);
  };
  runAsync(func, cb)(/* arguments for func */);
};
```

#### Using `this.async`

```js
printAfter(function () {
  var done = this.async();

  setTimeout(function () {
    done(null, "done running with callback");
  }, 10);
});
```

#### Returning a promise

```js
printAfter(function () {
  return new Promise(function (resolve, reject) {
    resolve("done running with promises");
  });
});
```

#### Synchronous function

```js
printAfter(function () {
  return "done running sync function";
});
```

#### Custom done factory

```js
var runAsync = require("run-async");

runAsync(function () {
  var callback = this.customAsync();
  callback(null, a + b);
}, "customAsync")(1, 2);
```

#### Passing context to async method

```js
var runAsync = require("run-async");

runAsync(function () {
  assert(this.isBound);
  var callback = this.async();
  callback(null, a + b);
}).call({ isBound: true }, 1, 2);
```

### runAsync.cb

`runAsync.cb` supports all the function types that `runAsync` does and additionally a traditional **callback as the last argument** signature:

```js
var runAsync = require("run-async");

// IMPORTANT: The wrapped function must have a fixed number of parameters.
runAsync.cb(
  function (a, b, cb) {
    cb(null, a + b);
  },
  function (err, result) {
    console.log(result);
  },
)(1, 2);
```

If your version of node support Promises natively (node >= 0.12), `runAsync` will return a promise. Example: `runAsync(func)(arg1, arg2).then(cb)`

# Licence

Copyright (c) 2014 Simon Boudrias (twitter: @vaxilart)  
Licensed under the MIT license.

---
_Source: https://npm.io/package/run-async · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
