# babel-plugin-transform-async-to-promises

> Transform async/await to promise chains

Latest version **0.8.18** (published 2021-12-29) · MIT license · 0 weekly downloads

## Install

```sh
npm install babel-plugin-transform-async-to-promises
pnpm add babel-plugin-transform-async-to-promises
yarn add babel-plugin-transform-async-to-promises
bun add babel-plugin-transform-async-to-promises
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.8.18 |
| Published | 2021-12-29 |
| First published | 2018-01-06 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 390.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 248 |
| Author | Ryan Petrich |
| Maintainers | rpetrich |
| Keywords | async, await, optimization, babel, compile, transform, minify |

## Links

- npm: https://www.npmjs.com/package/babel-plugin-transform-async-to-promises
- Repository: https://github.com/rpetrich/babel-plugin-transform-async-to-promises
- Homepage: https://github.com/rpetrich/babel-plugin-transform-async-to-promises#readme
- Issues: https://github.com/rpetrich/babel-plugin-transform-async-to-promises/issues
- npm.io page: https://npm.io/package/babel-plugin-transform-async-to-promises

## Alternatives

- [@openai/codex-sdk](https://npm.io/package/@openai/codex-sdk.md) — 731.4K weekly downloads
- [babel-plugin-transform-react-jsx](https://npm.io/package/babel-plugin-transform-react-jsx.md) — 565.0K weekly downloads
- [babel-helper-remove-or-void](https://npm.io/package/babel-helper-remove-or-void.md) — 508.5K weekly downloads
- [@pnpm/store-controller-types](https://npm.io/package/@pnpm/store-controller-types.md) — 186.9K weekly downloads
- [react-native-signature-canvas](https://npm.io/package/react-native-signature-canvas.md) — 155.6K weekly downloads

## Recent versions

- 0.8.18 (latest) — 2021-12-29
- 0.8.17 — 2021-12-20
- 0.8.16 — 2021-11-21
- 0.8.15 — 2019-10-26
- 0.8.14 — 2019-08-04
- 0.8.13 — 2019-07-20
- 0.8.12 — 2019-06-11
- 0.8.11 — 2019-05-26
- 0.8.10 — 2019-04-20
- 0.8.9 — 2019-04-08
- 0.8.8 — 2019-04-01
- 0.8.7 — 2019-03-24
- 0.8.6 — 2019-03-15
- 0.8.5 — 2019-02-24
- 0.8.4 — 2018-12-16
- … 21 more at https://npm.io/package/babel-plugin-transform-async-to-promises/versions

## README

babel-plugin-transform-async-to-promises
========================================

Babel plugin to transform `async` functions containing `await` expressions to the equivalent chain of `Promise` calls with use of minimal helper functions.

### Input:

```javascript
async function fetchAsObjectURL(url) {
    const response = await fetch(url);
    const blob = await response.blob();
    return URL.createObjectURL(blob);
}
```

### Output:

```javascript
const fetchAsObjectURL = _async(function(url) {
	return _await(fetch(url), function(response) {
		return _await(response.blob(), URL.createObjectURL);
	});
});
```

### Output with `hoist` enabled:

```javascript
function _response$blob(response) {
	return _await(response.blob(), URL.createObjectURL);
}
const fetchAsObjectURL = _async(function(url) {
	return _await(fetch(url), _response$blob);
});
```

### Output with `inlineHelpers` enabled:

```javascript
const fetchAsObjectURL = function(url) {
	try {
		return Promise.resolve(fetch(url)).then(function(response) {
			return Promise.resolve(response.blob()).then(URL.createObjectURL);
		});
	} catch(e) {
		return Promise.reject(e);
	}
}
```

### Output with `externalHelpers` enabled:

In the normal case, helpers are added to the top of the file for the `_async` and `_await` functions (as well as others). This can cause bloat in a codebase due to duplication of helper code in every file. To avoid this, enable `externalHelpers` and those will be imported instead:

```javascript
import { _async } from "babel-plugin-transform-async-to-promises/helpers";
import { _await } from "babel-plugin-transform-async-to-promises/helpers";

const fetchAsObjectURL = _async(function(url) {
	return _await(fetch(url), function(response) {
		return _await(response.blob(), URL.createObjectURL);
	});
});

export default fetchAsObjectURL;
```

## JavaScript Language Features

### Full Support
- `async`/`await`
- `for`/`while`/`do` loops (including loops that would exhaust stack if dispatched recursively)
- `switch` statements (including fallthrough and `default` cases)
- conditional expressions
- logical expressions
- `try`/`catch`
- `break`/`continue` statements (on both loops and labeled statements)
- `throw` expressions
- Function hoisting
- Variable hoisting
- Arrow functions
- Methods
- `arguments`
- `this`
- Proper member dereference order of operations
- Standards-compliant event loop scheduling

### Partial Support
- `Function.length`: `async` functions will often return a length of 0 (when the `_async` wrapper is used)
- Top level await support is experimental with compatible module bundler. Set `topLevelAwait` option to `return` when using SystemJS.

### No support
- `eval`: impossible to support without deep hooks into the runtime
- Async generator functions: not implemented or planned
- `Function.name`: rewrite pass removes function name instrumentation
- `new AsyncFunction(...)`: impossible to support without shipping babel and the plugin in the output

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