Skip to content

Latest commit

 

History

History
79 lines (59 loc) · 3.03 KB

topromise.md

File metadata and controls

79 lines (59 loc) · 3.03 KB

Rx.Observable.toPromise([promiseCtor])

Converts an Observable sequence to a ES2015 compliant promise.

Arguments

  1. [promiseCtor] (Promise): An ES2015 compliant Promise that can be created. This is optional. If your runtime already supports ES2015 promises, you do not need to fill in this parameter. Alternatively you can specify an ES2015 complaint promise via Rx.config.Promise

Returns

(Promise): An ES2015 compliant promise which contains the last value from the Observable sequence. If the Observable sequence is in error, then the Promise will be in the rejected stage. If the sequence is empty, the Promise will not resolve.

Example

/* Using normal ES2015 */
let source = Rx.Observable
  .just(42)
  .toPromise();

source.then((value) => console.log('Value: %s', value));
// => Value: 42

/* Rejected Promise */
/* Using normal ES2015 */
let source = Rx.Observable
  .throw(new Error('woops'))
  .toPromise();

source
  .then((value) => console.log('Value: %s', value))
  .catch((err) => console.log('Error: %s', err));
// => Error: Error: woops

/* Setting via the config */
Rx.config.Promise = RSVP.Promise;

let source = Rx.Observable
  .just(42)
  .toPromise();

source.then((value) => console.log('Value: %s', value));
// => Value: 42


/* Setting via the method */
let source = Rx.Observable
  .just(42)
  .toPromise(RSVP.Promise);

source.then((value) => console.log('Value: %s', value));
// => Value: 42

Location

File:

Dist:

Prerequisites:

NPM Packages:

NuGet Packages:

Unit Tests: