@grainrigi/async-singleton
TypeScript icon, indicating that this package has built-in type declarations

1.1.1 • Public • Published

async-singleton

Usage

import singletonAsync from '@grainrigi/async-singleton';

const func = singletonAsync(function() {
  return new Promise(resolve => { console.log('hoge'); resolve(); });
});

(async () => {
  func();
  func();
  await func();

  console.log('awaited');

  func();
  await func();
})();

/* Result
hoge
awaited
hoge
*/

Argument comparison and slicing

// function calls with different arguments are not aggregated
import singletonAsync from '@grainrigi/async-singleton';

const func = singletonAsync(function(arg1) {
  return new Promise(resolve => { console.log(arg1); resolve(); })
});

func(0);
func(1);
func(2);

/* Result
0
1
2
*/

// compare only sliced range
const func = singletonAsync(function(arg1, arg2) {
  return new Promise(resolve => { console.log(arg1, arg2); resolve(); })
}, { slice: [1, 2] });

func(0, 0);
func(1, 0);
func(1, 1);

/* Result
0 0
1 1
*/

Acknowledgement

The implementation is based on shokai/async-singleton.

Readme

Keywords

none

Package Sidebar

Install

npm i @grainrigi/async-singleton

Weekly Downloads

29

Version

1.1.1

License

MIT

Unpacked Size

185 kB

Total Files

9

Last publish

Collaborators

  • grainrigi