RxJS .toPromise returns only one value

Kevin

I'm using RxJS 6.2.1 and I'm trying to convert an observable to a promise

If I create a simple range observable using subscribe, I get values emitted over time.

import { range } from 'rxjs'

const source$ = range(0, 10);
source$
  .subscribe(
    x => console.log(x),
    err => console.log(err),
    () => console.log('complete')
  )

But how do I convert this whole thing back to a promise? I tried .toPromise() but I only get back a single value, which is "9"

import { range } from 'rxjs'

const source$ = range(0, 10);
source$
  .toPromise()
  .then(x => console.log('x', x)) . //only get back "9"
  .catch(err => console.log('err', err))

How would I rewrite this so that I get back an array from 0 through 9?

kctang

This will turn all values from your observable stream into an array and resolve the array as a promise value.

of('a','b','c','d').pipe(
  toArray()
).toPromise().then(val => console.log(val))

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Mongo find query only returns one result

C# query inside a for loop returns only one value

Have pyodbc return a simple (scalar) value for a query that only returns one item

Function returns only first value

SQLAlchemy _asdict() method returns only one column

How to call multiple URLs and only handle the first value that returns using RxJS?

MySQL COUNT query returns only one value from a CASE expression

RXJS subscribe only if previous value is not that great and I really need a better one

Lists returns only the first value

Discord channel members only returns one member?

Need to read value of multiple node: ORA-19025: EXTRACTVALUE returns value of only one node

matlab constant anonymous function returns only one value instead of an array

Mysqli loop, returns only one

IF statement on ORDER BY returns only one value

Foreach returns only one output

Spring Data Neo4j neo4jTemplate.fetch() only returns one value

SSRS LookupSet returns only one kind of value

$.getJSON returns only one entry

Unpacking a binary file with Python only returns one value

ResultSet returns only one row

Array only returns one element

Can RxJS Observables be only handled by one subscriber?

Fetching data from Core Data only returns one value

nrow returns null value for only one row in R

RXJS in Angular 6 returns only { "_isScalar": false }

Vuejs computed property for loop print all values, but returns only one value

MySQL select MAX value from subquery only returns one results from outer query. Why?

Binary Search only returns one statement

Rxjs toPromise() deprecated

TOP Ranking

HotTag

Archive