Skip to content

Function: asPromise()

asPromise<T>(subject, expectedValue?, timeoutOrOptions?): IPromisE_Timeout<T>

Defined in: packages/rx/src/asPromise.ts:45

Type Parameters

T

T = unknown

Parameters

subject

RxJS subject or observable

Subscribable<T> | SubjectLike<T>

expectedValue?

(optional) if undefined, will resolve as soon as any value is received. If function, it should return true or false to indicate whether the value should be resolved.

T | (value) => boolean

timeoutOrOptions?

(optional)

number | Omit<TimeoutOptions, "batchFunc"> & object

Returns

IPromisE_Timeout<T>

timeout promise

Example

Create a promise using RxJS subject

typescript
import { BehaviorSubject, subjectAsPromise } from '@superutils/rx'

const subject = new BehaviorSubject(0)
setInterval(() => subject.next(subject.value + 1), 1000)

// resolve conditionally based on value received
subjectAsPromise(subject, value => value >= 3)
	.then(value => console.log('Expected >= 3, received ', value))

// resolve when only specific value is received
subjectAsPromise(subject, 5).then(value => console.log('Expected 5, received ', value))