Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/ert78gb/async-init
Async Singleton Initializer
https://github.com/ert78gb/async-init
Last synced: 13 days ago
JSON representation
Async Singleton Initializer
- Host: GitHub
- URL: https://github.com/ert78gb/async-init
- Owner: ert78gb
- Created: 2021-03-20T17:23:18.000Z (almost 4 years ago)
- Default Branch: master
- Last Pushed: 2023-06-12T22:57:40.000Z (over 1 year ago)
- Last Synced: 2024-04-26T04:03:51.268Z (8 months ago)
- Language: JavaScript
- Size: 223 KB
- Stars: 1
- Watchers: 2
- Forks: 1
- Open Issues: 3
-
Metadata Files:
- Readme: readme.md
Awesome Lists containing this project
README
# Async init
The library helps to initialize async singleton.
If multiple requests arrive before singleton initialization is finished, then they will be queued.
Once singleton has been initialized the queue will be processed in the FIFO.## Usage
`$ npm i @ert78gb/async-init`
```javascript
// singleton.js
const asyncInit = require('@ert78gb/async-init')
const asyncIniter = asyncInit();async function asyncInstanceCreator() {
// implementation
// It will call only once
return Promise.resolve('Return value of an async operation.')
}function getSingleton() {
return asyncIniter(asyncInstanceCreator)
}module.exports = getSingleton
// usage.js
const getSingleton = require('./singleton.js')const [instance1, instance2] = Promise.all([
getSingleton(),
getSingleton()
])console.log(instance1 === instance2) // true
```