# await_once

> Implement of singleton mode -- call an async function only once.

Latest version **2.0.7** (published 2019-04-01) · 996 License license · 0 weekly downloads

## Install

```sh
npm install await_once
pnpm add await_once
yarn add await_once
bun add await_once
```

## Health

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

Positive: no vulnerabilities.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.0.7 |
| Published | 2019-04-01 |
| First published | 2018-06-20 |
| Weekly downloads | 0 |
| License | 996 License |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 7.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | xhawk18@gmail.com |
| Maintainers | xhawk18 |
| Keywords | singleton, once, await, one, instance |

## Links

- npm: https://www.npmjs.com/package/await_once
- Repository: https://github.com/xhawk18/await_once
- Homepage: https://github.com/xhawk18/await_once#readme
- Issues: https://github.com/xhawk18/await_once/issues
- npm.io page: https://npm.io/package/await_once

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

- 2.0.7 (latest) — 2019-04-01
- 2.0.6 — 2018-06-26
- 2.0.5 — 2018-06-21
- 2.0.4 — 2018-06-21
- 2.0.3 — 2018-06-20
- 2.0.2 — 2018-06-20
- 2.0.1 — 2018-06-20

## README

# Description

This module implements singleton mode and helps to (await) call async function only once.

# Api

```javascript
let once = require('await_once');
await once(name, func);
```

* name - a custom defined unique name.
* func - an async or sync function.

# Demo

[Try it online](https://runkit.com/embed/fypy7r84a9e5)

# Usage

## Example 1 - call an async function once and await the same result

```javascript
let once = require('await_once');
let util = require('util');

async function myFunc1(){
    console.log('before setTimeout');
    await util.promisify(setTimeout)(1000);

    let ret = Math.random();
    console.log('after setTimeout will return', ret);
    return ret;
}

async function main(){
   let ret = await once('my_name1', myFunc1);
   console.log('ret =', ret);
}

main();
main();
main();
setTimeout(main, 3000);
```

## Example 2 - call an async function once and catch the same error

```javascript
let once = require('await_once');
let util = require('util');

async function myFunc2(){
    console.log('before setTimeout');
    await util.promisify(setTimeout)(1000);

    let ret = Math.random();
    console.log('after setTimeout will throw err', ret);
    throw new Error(ret);
}

async function main() {
    try {
        let ret = await once('my_name2', myFunc2);
        console.log('ret =', ret);  //will not run to here
    } catch(err) {
        console.log('err = ', err);
    }
}

main();
main();
main();
setTimeout(main, 3000);
```

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