Wrap a promise as a singleton, returning the same promise on multiple calls.
Through the singleton.reset() method, you can reset the currently expired promise and wait for it to close normally.
singleton.reset()
将一个 promise 包装为单例,在多次调用时,返回同一个 promise
通过 singleton.reset 方法可以重置当前过期的 promise,并等待其正常关闭
singleton.reset
the function that returns the promise to wrap - 要包装的 promise 返回函数
a function that returns the wrapped promise - 返回包装后的 promise
let i = 1const singleton = createSingletonPromise(() => Promise.resolve(i++))const p1 = singleton()const p2 = singleton()console.log(p1 === p2) // trueawait p1() // will log: 2await p2() // will log: 2 Copy
let i = 1const singleton = createSingletonPromise(() => Promise.resolve(i++))const p1 = singleton()const p2 = singleton()console.log(p1 === p2) // trueawait p1() // will log: 2await p2() // will log: 2
let i = 0const singleton = createSingletonPromise(() => Promise.resolve(i++))await singleton() // will log: 1await singleton.reset() // reset the promise, and await it to have proper shutdownawait singleton() // will log: 2 Copy
let i = 0const singleton = createSingletonPromise(() => Promise.resolve(i++))await singleton() // will log: 1await singleton.reset() // reset the promise, and await it to have proper shutdownawait singleton() // will log: 2
Wrap a promise as a singleton, returning the same promise on multiple calls.
Through the
singleton.reset()method, you can reset the currently expired promise and wait for it to close normally.将一个 promise 包装为单例,在多次调用时,返回同一个 promise
通过
singleton.reset方法可以重置当前过期的 promise,并等待其正常关闭