javascript - Async Observable method inside of a map method -
what's best approach transform array , call few observable methods inside.
what looking of type work:
resultarray = somearray.map(item => { oneobs.subscribe(result1 => { item.result1 = result1 }); secondobs.subscribe(result2 => { item.result2 = result2 }); }); what best approach here? came workaround use foreach loop of kind:
somearray.foreach(item => { observable.forkjoin([oneobs, secondobs]).subscribe( (results: [result1type, result2type]) => { item.result1 = results[0]; item.result2 = results[1]; resultarray.push(item); }); }); any appreciated.
your trying mix synchronous , asynchronous code, might lead weird behavior. recommendation convert array observable (making array asynchronous) getting results , converting completed result array again:
observable.from(somearray) // 1 .mergemap(item => observable.forkjoin(oneobs, twoobs) .map(results => { item.result1 = results[0]; item.result2 = results[1]; return item; })) .toarray() // 2 .subscribe(arr => resultarray = arr); - create observable based on array
- wait observables finish , create array resulting values
Comments
Post a Comment