# node-repl-await

> Standalone util function from Node.js core to process await statements in REPL.

Latest version **0.1.2** (published 2021-02-27) · MIT license · 0 weekly downloads

## Install

```sh
npm install node-repl-await
pnpm add node-repl-await
yarn add node-repl-await
bun add node-repl-await
```

## Health

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

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

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.2 |
| Published | 2021-02-27 |
| First published | 2019-04-18 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 5 |
| Unpacked size | 10.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 4 |
| Maintainers | hyurl |
| Keywords | repl, await, async, shell, console, terminal |

## Links

- npm: https://www.npmjs.com/package/node-repl-await
- Repository: https://github.com/hyurl/node-repl-await
- Homepage: https://nodejs.org
- Issues: https://github.com/nodejs/node/issues
- npm.io page: https://npm.io/package/node-repl-await

## Dependencies (5)

- [acorn](https://npm.io/package/acorn.md) ^8.0.5
- [acorn-walk](https://npm.io/package/acorn-walk.md) ^8.0.2
- [acorn-class-fields](https://npm.io/package/acorn-class-fields.md) ^1.0.0
- [acorn-private-methods](https://npm.io/package/acorn-private-methods.md) ^1.0.0
- [acorn-static-class-features](https://npm.io/package/acorn-static-class-features.md) ^1.0.0

## 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

- 0.1.2 (latest) — 2021-02-27
- 0.1.1 — 2019-11-12
- 0.1.0 — 2019-04-18

## README

# Node's processTopLevelAwait

Standalone util function from Node.js core to process await statements in REPL.

Since v10.0.0, Node.js introduced a new experimental feature to support `await` 
keyword in the REPL by using the argument `--experimental-repl-await`, however,
if a user wants to implement a custom REPL console, there would be no
await-support at all, to achieve such a goal, this package clones the internal
module of await-support to form a standalone version, allowing users share the 
benefits of await-support in their own REPL environments.

See
[Node.js docs](https://nodejs.org/dist/latest-v11.x/docs/api/repl.html#repl_await_keyword)
for more details, and contribute to the
[original source](https://github.com/nodejs/node/blob/master/lib/internal/repl/await.js).

## Example

```javascript
const repl = require("repl");
const vm = require("vm");
const { processTopLevelAwait } = require("node-repl-await");

function isRecoverableError(error) {
    if (error.name === 'SyntaxError') {
        return /^(Unexpected end of input|Unexpected token)/.test(error.message);
    }
    return false;
}

async function myEval(code, context, filename, callback) {
    code = processTopLevelAwait(code) || code;

    try {
        let result = await vm.runInNewContext(code, context);
        callback(null, result);
    } catch (e) {
        if (isRecoverableError(e)) {
            callback(new repl.Recoverable(e));
        } else {
            console.log(e);
        }
    }
}

repl.start({ prompt: '> ', eval: myEval });
```

## API

```typescript
function processTopLevelAwait(src: string): string | void
```

Tries to wrap the given source code to an immediately-invoked function if it 
contains any top level `await` statement in the form of

```js
(async () => { /* source code */ })()
```

If no `await` presents or processing failed, the function returns `null`.

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