Your example can be simplified to:
from([1, 2, 3]).pipe( concatMap(id => fetch(`url-to-api/${id}`) .then(res => res.json()) ), ).subscribe()
Operators expecting an ObservableInput<T>, such as concatMap, automatically coerce promises to observables. And observables built from promises automatically complete when the promise resolves.
ObservableInput<T>
concatMap
From the rxjs source code:
export type ObservableInput<T> = SubscribableOrPromise<T> | ArrayLike<T> | Iterable<T>;
If you wanted to modify the promise's response, you can do that with an async function. For example:
from([1, 2, 3]).pipe( concatMap(async id => { const res = await fetch(`url-to-api/${id}`); const json = await res.json(); // do stuff return json; }), ).subscribe()
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Your example can be simplified to:
Operators expecting an
ObservableInput<T>
, such asconcatMap
, automatically coerce promises to observables. And observables built from promises automatically complete when the promise resolves.From the rxjs source code:
If you wanted to modify the promise's response, you can do that with an async function. For example: