@nodesuite/defer
v0.3.32
Published
Deferred Promise resolution tools.
Downloads
9
Maintainers
Readme
@nodesuite/defer
Summary
Simple deferred Promise
generator.
Installation
pnpm install @nodesuite/defer
Usage
import { defer } from "@nodesuite/defer"
import { someEventfulEmitter } from "./events"
const { resolve, reject, untilResolved } = defer()
// Define async work...
someEventfulEmitter.on("someEvent", resolve)
// ...or some alternative trigger...
const secondEmitter = new EventEmitter()
secondEmitter.on("secondEvent", resolve)
// ...or abstract resoltion conditons...
class Foo {
#resolve
#reject
public constructor({ resolve, reject }) {
this.#resolve = resolve
this.#reject = reject
}
public someSuccessFunction() {
return this.#resolve()
}
public someFailureFunction() {
return this.#reject()
}
}
const foo = new Foo({ resolve, reject })
// Reject the master promise...
someEventfulEmitter.on("error", () => foo.someFailureFunction())
// Can await resolution of promise from any source.
await untilResolved()