Skip to content

Function: asPromise()

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

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

Type Parameters

T

T = unknown

Parameters

input$

Observable<T>

RxJS subject or observable

expectedValue?

T | ((value) => boolean)

(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.

timeoutOrOptions?

number | AsPromise_Options

(optional) timeout duration or options

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))