Hi sir, one doubt When converting a Promise into an Observable, we are using from() . Output of from() operator should be like : 10 20 30 40 50 but it is emitting like a whole array 10,20,30,40,50 (12:19 of video), which is the pattern of the of() operator So, we can say that while converting a Promise using from() , it will not iterate . Output will be like of the of() , not like of the from() operator ?
(The below answer is not my own, like many i also can't understand last example of the video which involved promise, so i asked the doubt in chatgpt and it provided the below answer, pasting it here so it may help people like me who didn't understand) In the context of the example provided, "resolve" is a method used to create a promise that is already resolved with a specified value. This is a feature of the Promise object in JavaScript. When using the from operator in RxJS to convert a promise to an observable, it behaves differently based on the type of the promise's resolved value. Here's a breakdown of the behavior: Promise that "resolves" to a single value: The from operator will emit that single value. Promise that "resolves" to an array: The from operator will emit the entire array as a single value, not individual elements. If we want to iterate over the elements of the array and emit each element separately, we need to first convert the promise to an observable, and then use the mergeMap operator to further process the emitted array into individual elements. example: import { from } from 'rxjs'; import { mergeMap } from 'rxjs/operators'; // A promise that resolves to an array const promise = Promise.resolve([10, 20, 30]); // Convert the promise to an observable and emit each element separately const observable = from(promise).pipe( mergeMap(array => from(array)) ); observable.subscribe(value => console.log(value)); output: 10 20 30
i have a question . when we convert promise to observable with from operator . from operator should oprated array in promis but it didnt? and we see all data without streaming it ?why
of operator will emit data one after other or, simultaneously. had confusion since in last lecture we kept time out to show some diff in time. so i thought you kept that since next is an observer.next is async function.
Hi sir, one doubt
When converting a Promise into an Observable, we are using from() .
Output of from() operator should be like :
10
20
30
40
50
but it is emitting like a whole array 10,20,30,40,50 (12:19 of video), which is the pattern of the of() operator
So, we can say that while converting a Promise using from() , it will not iterate .
Output will be like of the of() , not like of the from() operator ?
Also having this doubt. Manoj sir, can you please have a look?
Same doubt
(The below answer is not my own, like many i also can't understand last example of the video which involved promise, so i asked the doubt in chatgpt and it provided the below answer, pasting it here so it may help people like me who didn't understand)
In the context of the example provided, "resolve" is a method used to create a promise that is already resolved with a specified value. This is a feature of the Promise object in JavaScript.
When using the from operator in RxJS to convert a promise to an observable, it behaves differently based on the type of the promise's resolved value.
Here's a breakdown of the behavior:
Promise that "resolves" to a single value: The from operator will emit that single value.
Promise that "resolves" to an array: The from operator will emit the entire array as a single value, not individual elements.
If we want to iterate over the elements of the array and emit each element separately, we need to first convert the promise to an observable, and then use the mergeMap operator to further process the emitted array into individual elements.
example:
import { from } from 'rxjs';
import { mergeMap } from 'rxjs/operators';
// A promise that resolves to an array
const promise = Promise.resolve([10, 20, 30]);
// Convert the promise to an observable and emit each element separately
const observable = from(promise).pipe(
mergeMap(array => from(array))
);
observable.subscribe(value => console.log(value));
output:
10
20
30
Sir,
Please do NgRx series as well. Will be really helpful for career growth
Like for java durga sir is best and for angular you r the best rq
i have a question . when we convert promise to observable with from operator . from operator should oprated array in promis but it didnt? and we see all data without streaming it ?why
of operator will emit data one after other or, simultaneously. had confusion since in last lecture we kept time out to show some diff in time. so i thought you kept that since next is an observer.next is async function.