npm.io
2.0.7 • Published 7 years ago

await_once

Licence
996 License
Version
2.0.7
Deps
0
Size
8 kB
Vulns
0
Weekly
0

Description

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

Api

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

Usage

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

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

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);

Keywords