http request within Observable happens even without subscription












2















I'm seeing the http request happen - in the Chrome Network tab, and also by using then() within the fetch promise - with the below code, despite never subscribing to the inner observable (saveCourses$).



I modified to use from() rather than fromPromise() as per the updated rxjs documentation, but the same occurs.



My understanding is that the inner Observable should not run?



ngOnInit() {
this.form.valueChanges.pipe(
filter(() => this.form.valid)
)
.subscribe(changes => {
const saveCourses$ = fromPromise(fetch(`/api/courses/${this.course.id}`, {
method: 'PUT',
body: JSON.stringify(changes),
headers: {
'content-type': 'application/json'
}
}));
});
}









share|improve this question























  • The fromPromise method is most likely using the promise right away in order to convert it into an observable. Why aren't you using the built in Angular http stuff that does things the way you are wanting?

    – Daniel W Strimpel
    Nov 13 '18 at 18:52











  • I'm following an Angular University course on rxjs - I don't intend to use fetch in any projects, but for this example I understand the http request shouldn't be happening without the subscription (according to the course author who hasn't yet come back to me). I'd really like to understand why this auto subscription is happening

    – DJC
    Nov 13 '18 at 19:39











  • As stated, it is happening in the fromPromise code. The callback passed to the Observable in the code is calling the then method right away. github.com/ReactiveX/rxjs/blob/…

    – Daniel W Strimpel
    Nov 13 '18 at 20:00
















2















I'm seeing the http request happen - in the Chrome Network tab, and also by using then() within the fetch promise - with the below code, despite never subscribing to the inner observable (saveCourses$).



I modified to use from() rather than fromPromise() as per the updated rxjs documentation, but the same occurs.



My understanding is that the inner Observable should not run?



ngOnInit() {
this.form.valueChanges.pipe(
filter(() => this.form.valid)
)
.subscribe(changes => {
const saveCourses$ = fromPromise(fetch(`/api/courses/${this.course.id}`, {
method: 'PUT',
body: JSON.stringify(changes),
headers: {
'content-type': 'application/json'
}
}));
});
}









share|improve this question























  • The fromPromise method is most likely using the promise right away in order to convert it into an observable. Why aren't you using the built in Angular http stuff that does things the way you are wanting?

    – Daniel W Strimpel
    Nov 13 '18 at 18:52











  • I'm following an Angular University course on rxjs - I don't intend to use fetch in any projects, but for this example I understand the http request shouldn't be happening without the subscription (according to the course author who hasn't yet come back to me). I'd really like to understand why this auto subscription is happening

    – DJC
    Nov 13 '18 at 19:39











  • As stated, it is happening in the fromPromise code. The callback passed to the Observable in the code is calling the then method right away. github.com/ReactiveX/rxjs/blob/…

    – Daniel W Strimpel
    Nov 13 '18 at 20:00














2












2








2








I'm seeing the http request happen - in the Chrome Network tab, and also by using then() within the fetch promise - with the below code, despite never subscribing to the inner observable (saveCourses$).



I modified to use from() rather than fromPromise() as per the updated rxjs documentation, but the same occurs.



My understanding is that the inner Observable should not run?



ngOnInit() {
this.form.valueChanges.pipe(
filter(() => this.form.valid)
)
.subscribe(changes => {
const saveCourses$ = fromPromise(fetch(`/api/courses/${this.course.id}`, {
method: 'PUT',
body: JSON.stringify(changes),
headers: {
'content-type': 'application/json'
}
}));
});
}









share|improve this question














I'm seeing the http request happen - in the Chrome Network tab, and also by using then() within the fetch promise - with the below code, despite never subscribing to the inner observable (saveCourses$).



I modified to use from() rather than fromPromise() as per the updated rxjs documentation, but the same occurs.



My understanding is that the inner Observable should not run?



ngOnInit() {
this.form.valueChanges.pipe(
filter(() => this.form.valid)
)
.subscribe(changes => {
const saveCourses$ = fromPromise(fetch(`/api/courses/${this.course.id}`, {
method: 'PUT',
body: JSON.stringify(changes),
headers: {
'content-type': 'application/json'
}
}));
});
}






javascript angular ecmascript-6 rxjs6






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 13 '18 at 17:31









DJCDJC

601821




601821













  • The fromPromise method is most likely using the promise right away in order to convert it into an observable. Why aren't you using the built in Angular http stuff that does things the way you are wanting?

    – Daniel W Strimpel
    Nov 13 '18 at 18:52











  • I'm following an Angular University course on rxjs - I don't intend to use fetch in any projects, but for this example I understand the http request shouldn't be happening without the subscription (according to the course author who hasn't yet come back to me). I'd really like to understand why this auto subscription is happening

    – DJC
    Nov 13 '18 at 19:39











  • As stated, it is happening in the fromPromise code. The callback passed to the Observable in the code is calling the then method right away. github.com/ReactiveX/rxjs/blob/…

    – Daniel W Strimpel
    Nov 13 '18 at 20:00



















  • The fromPromise method is most likely using the promise right away in order to convert it into an observable. Why aren't you using the built in Angular http stuff that does things the way you are wanting?

    – Daniel W Strimpel
    Nov 13 '18 at 18:52











  • I'm following an Angular University course on rxjs - I don't intend to use fetch in any projects, but for this example I understand the http request shouldn't be happening without the subscription (according to the course author who hasn't yet come back to me). I'd really like to understand why this auto subscription is happening

    – DJC
    Nov 13 '18 at 19:39











  • As stated, it is happening in the fromPromise code. The callback passed to the Observable in the code is calling the then method right away. github.com/ReactiveX/rxjs/blob/…

    – Daniel W Strimpel
    Nov 13 '18 at 20:00

















The fromPromise method is most likely using the promise right away in order to convert it into an observable. Why aren't you using the built in Angular http stuff that does things the way you are wanting?

– Daniel W Strimpel
Nov 13 '18 at 18:52





The fromPromise method is most likely using the promise right away in order to convert it into an observable. Why aren't you using the built in Angular http stuff that does things the way you are wanting?

– Daniel W Strimpel
Nov 13 '18 at 18:52













I'm following an Angular University course on rxjs - I don't intend to use fetch in any projects, but for this example I understand the http request shouldn't be happening without the subscription (according to the course author who hasn't yet come back to me). I'd really like to understand why this auto subscription is happening

– DJC
Nov 13 '18 at 19:39





I'm following an Angular University course on rxjs - I don't intend to use fetch in any projects, but for this example I understand the http request shouldn't be happening without the subscription (according to the course author who hasn't yet come back to me). I'd really like to understand why this auto subscription is happening

– DJC
Nov 13 '18 at 19:39













As stated, it is happening in the fromPromise code. The callback passed to the Observable in the code is calling the then method right away. github.com/ReactiveX/rxjs/blob/…

– Daniel W Strimpel
Nov 13 '18 at 20:00





As stated, it is happening in the fromPromise code. The callback passed to the Observable in the code is calling the then method right away. github.com/ReactiveX/rxjs/blob/…

– Daniel W Strimpel
Nov 13 '18 at 20:00












2 Answers
2






active

oldest

votes


















2














While it is common for Observable-based interfaces to defer expensive work (like network calls) until they are subscribed, that's not strictly necessarily. Sometimes, a method that returns an Observable will already have done, or begun, a lot of work even before its subscribe method is called. If the Observable does work before its is subscribed, it's often referred to as "hot" (as opposed to "cold" Observables that don't do work unless subscribers are attached). See the "Hot and Cold Observables" section here.



Promise-based interfaces work a lot like "hot" Observables. That is, we expect that the potentially expensive operation will be started as soon as we call the function that gives us the Promise. We learn about the result of the operation when the Promise is resolved, but it will resolve whether or not we actually listen to it (i.e., hook up a "then" handler to the Promise).



In your example, your Observable is created with the fromPromise method:



fromPromise(fetch(`/api/courses/${this.course.id}`...)


fromPromise works, essentially, by waiting for a Promise to be resolved, and when it is, emitting the Promise's resolved value via a newly-created Observable. The important thing here is that fromPromise has to call fetch() in order to get the Promise from which the Observable is built. And the act of calling fetch causes the network call to be initiated.



So in this case, but unlike most "typical" uses of Observables for network or other long operations, you needn't call .subscribe() for the operation to be executed. It will execute as soon as fetch is called, and fetch is called when the Observable is created, not when subscribed.






share|improve this answer
























  • thanks for the clear and detailed explanation. One more quick point - I created a function that calls fetch within an Observable.create() method, and the http request is in that case NOT triggered unless I explicitly subscribe - so this means it is in particular the combination of fromPromise and fetch that causes the problem in the original question, rather than just using fetch, is that correct?

    – DJC
    Nov 14 '18 at 10:22











  • @DJC Right, the difference is that the function that you pass to Observable.create is only called when the Observable is subscribed. Contrast this with the behavior of fromPromise, with which the supplied function is called when the Observable is created. reactivex.io/documentation/operators/create.html

    – Myk Willis
    Nov 14 '18 at 15:02











  • Perfect - thanks a lot for the detailed explanation. Turns out Angular University got it wrong ;)

    – DJC
    Nov 14 '18 at 15:10



















0














Stop using fetch and use HttpClient. If you open a console and type fetch('foo') you will see a net request. It's the invocation before fromPromise that's the issue.






share|improve this answer
























  • I'm following an Angular University course on rxjs - I don't intend to use fetch in any projects, but for this example I understand the http request shouldn't be happening without the subscription (according to the course author who hasn't yet come back to me). I'd really like to understand why this auto subscription is happening

    – DJC
    Nov 13 '18 at 19:39











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53286573%2fhttp-request-within-observable-happens-even-without-subscription%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














While it is common for Observable-based interfaces to defer expensive work (like network calls) until they are subscribed, that's not strictly necessarily. Sometimes, a method that returns an Observable will already have done, or begun, a lot of work even before its subscribe method is called. If the Observable does work before its is subscribed, it's often referred to as "hot" (as opposed to "cold" Observables that don't do work unless subscribers are attached). See the "Hot and Cold Observables" section here.



Promise-based interfaces work a lot like "hot" Observables. That is, we expect that the potentially expensive operation will be started as soon as we call the function that gives us the Promise. We learn about the result of the operation when the Promise is resolved, but it will resolve whether or not we actually listen to it (i.e., hook up a "then" handler to the Promise).



In your example, your Observable is created with the fromPromise method:



fromPromise(fetch(`/api/courses/${this.course.id}`...)


fromPromise works, essentially, by waiting for a Promise to be resolved, and when it is, emitting the Promise's resolved value via a newly-created Observable. The important thing here is that fromPromise has to call fetch() in order to get the Promise from which the Observable is built. And the act of calling fetch causes the network call to be initiated.



So in this case, but unlike most "typical" uses of Observables for network or other long operations, you needn't call .subscribe() for the operation to be executed. It will execute as soon as fetch is called, and fetch is called when the Observable is created, not when subscribed.






share|improve this answer
























  • thanks for the clear and detailed explanation. One more quick point - I created a function that calls fetch within an Observable.create() method, and the http request is in that case NOT triggered unless I explicitly subscribe - so this means it is in particular the combination of fromPromise and fetch that causes the problem in the original question, rather than just using fetch, is that correct?

    – DJC
    Nov 14 '18 at 10:22











  • @DJC Right, the difference is that the function that you pass to Observable.create is only called when the Observable is subscribed. Contrast this with the behavior of fromPromise, with which the supplied function is called when the Observable is created. reactivex.io/documentation/operators/create.html

    – Myk Willis
    Nov 14 '18 at 15:02











  • Perfect - thanks a lot for the detailed explanation. Turns out Angular University got it wrong ;)

    – DJC
    Nov 14 '18 at 15:10
















2














While it is common for Observable-based interfaces to defer expensive work (like network calls) until they are subscribed, that's not strictly necessarily. Sometimes, a method that returns an Observable will already have done, or begun, a lot of work even before its subscribe method is called. If the Observable does work before its is subscribed, it's often referred to as "hot" (as opposed to "cold" Observables that don't do work unless subscribers are attached). See the "Hot and Cold Observables" section here.



Promise-based interfaces work a lot like "hot" Observables. That is, we expect that the potentially expensive operation will be started as soon as we call the function that gives us the Promise. We learn about the result of the operation when the Promise is resolved, but it will resolve whether or not we actually listen to it (i.e., hook up a "then" handler to the Promise).



In your example, your Observable is created with the fromPromise method:



fromPromise(fetch(`/api/courses/${this.course.id}`...)


fromPromise works, essentially, by waiting for a Promise to be resolved, and when it is, emitting the Promise's resolved value via a newly-created Observable. The important thing here is that fromPromise has to call fetch() in order to get the Promise from which the Observable is built. And the act of calling fetch causes the network call to be initiated.



So in this case, but unlike most "typical" uses of Observables for network or other long operations, you needn't call .subscribe() for the operation to be executed. It will execute as soon as fetch is called, and fetch is called when the Observable is created, not when subscribed.






share|improve this answer
























  • thanks for the clear and detailed explanation. One more quick point - I created a function that calls fetch within an Observable.create() method, and the http request is in that case NOT triggered unless I explicitly subscribe - so this means it is in particular the combination of fromPromise and fetch that causes the problem in the original question, rather than just using fetch, is that correct?

    – DJC
    Nov 14 '18 at 10:22











  • @DJC Right, the difference is that the function that you pass to Observable.create is only called when the Observable is subscribed. Contrast this with the behavior of fromPromise, with which the supplied function is called when the Observable is created. reactivex.io/documentation/operators/create.html

    – Myk Willis
    Nov 14 '18 at 15:02











  • Perfect - thanks a lot for the detailed explanation. Turns out Angular University got it wrong ;)

    – DJC
    Nov 14 '18 at 15:10














2












2








2







While it is common for Observable-based interfaces to defer expensive work (like network calls) until they are subscribed, that's not strictly necessarily. Sometimes, a method that returns an Observable will already have done, or begun, a lot of work even before its subscribe method is called. If the Observable does work before its is subscribed, it's often referred to as "hot" (as opposed to "cold" Observables that don't do work unless subscribers are attached). See the "Hot and Cold Observables" section here.



Promise-based interfaces work a lot like "hot" Observables. That is, we expect that the potentially expensive operation will be started as soon as we call the function that gives us the Promise. We learn about the result of the operation when the Promise is resolved, but it will resolve whether or not we actually listen to it (i.e., hook up a "then" handler to the Promise).



In your example, your Observable is created with the fromPromise method:



fromPromise(fetch(`/api/courses/${this.course.id}`...)


fromPromise works, essentially, by waiting for a Promise to be resolved, and when it is, emitting the Promise's resolved value via a newly-created Observable. The important thing here is that fromPromise has to call fetch() in order to get the Promise from which the Observable is built. And the act of calling fetch causes the network call to be initiated.



So in this case, but unlike most "typical" uses of Observables for network or other long operations, you needn't call .subscribe() for the operation to be executed. It will execute as soon as fetch is called, and fetch is called when the Observable is created, not when subscribed.






share|improve this answer













While it is common for Observable-based interfaces to defer expensive work (like network calls) until they are subscribed, that's not strictly necessarily. Sometimes, a method that returns an Observable will already have done, or begun, a lot of work even before its subscribe method is called. If the Observable does work before its is subscribed, it's often referred to as "hot" (as opposed to "cold" Observables that don't do work unless subscribers are attached). See the "Hot and Cold Observables" section here.



Promise-based interfaces work a lot like "hot" Observables. That is, we expect that the potentially expensive operation will be started as soon as we call the function that gives us the Promise. We learn about the result of the operation when the Promise is resolved, but it will resolve whether or not we actually listen to it (i.e., hook up a "then" handler to the Promise).



In your example, your Observable is created with the fromPromise method:



fromPromise(fetch(`/api/courses/${this.course.id}`...)


fromPromise works, essentially, by waiting for a Promise to be resolved, and when it is, emitting the Promise's resolved value via a newly-created Observable. The important thing here is that fromPromise has to call fetch() in order to get the Promise from which the Observable is built. And the act of calling fetch causes the network call to be initiated.



So in this case, but unlike most "typical" uses of Observables for network or other long operations, you needn't call .subscribe() for the operation to be executed. It will execute as soon as fetch is called, and fetch is called when the Observable is created, not when subscribed.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 14 '18 at 1:06









Myk WillisMyk Willis

5,90411936




5,90411936













  • thanks for the clear and detailed explanation. One more quick point - I created a function that calls fetch within an Observable.create() method, and the http request is in that case NOT triggered unless I explicitly subscribe - so this means it is in particular the combination of fromPromise and fetch that causes the problem in the original question, rather than just using fetch, is that correct?

    – DJC
    Nov 14 '18 at 10:22











  • @DJC Right, the difference is that the function that you pass to Observable.create is only called when the Observable is subscribed. Contrast this with the behavior of fromPromise, with which the supplied function is called when the Observable is created. reactivex.io/documentation/operators/create.html

    – Myk Willis
    Nov 14 '18 at 15:02











  • Perfect - thanks a lot for the detailed explanation. Turns out Angular University got it wrong ;)

    – DJC
    Nov 14 '18 at 15:10



















  • thanks for the clear and detailed explanation. One more quick point - I created a function that calls fetch within an Observable.create() method, and the http request is in that case NOT triggered unless I explicitly subscribe - so this means it is in particular the combination of fromPromise and fetch that causes the problem in the original question, rather than just using fetch, is that correct?

    – DJC
    Nov 14 '18 at 10:22











  • @DJC Right, the difference is that the function that you pass to Observable.create is only called when the Observable is subscribed. Contrast this with the behavior of fromPromise, with which the supplied function is called when the Observable is created. reactivex.io/documentation/operators/create.html

    – Myk Willis
    Nov 14 '18 at 15:02











  • Perfect - thanks a lot for the detailed explanation. Turns out Angular University got it wrong ;)

    – DJC
    Nov 14 '18 at 15:10

















thanks for the clear and detailed explanation. One more quick point - I created a function that calls fetch within an Observable.create() method, and the http request is in that case NOT triggered unless I explicitly subscribe - so this means it is in particular the combination of fromPromise and fetch that causes the problem in the original question, rather than just using fetch, is that correct?

– DJC
Nov 14 '18 at 10:22





thanks for the clear and detailed explanation. One more quick point - I created a function that calls fetch within an Observable.create() method, and the http request is in that case NOT triggered unless I explicitly subscribe - so this means it is in particular the combination of fromPromise and fetch that causes the problem in the original question, rather than just using fetch, is that correct?

– DJC
Nov 14 '18 at 10:22













@DJC Right, the difference is that the function that you pass to Observable.create is only called when the Observable is subscribed. Contrast this with the behavior of fromPromise, with which the supplied function is called when the Observable is created. reactivex.io/documentation/operators/create.html

– Myk Willis
Nov 14 '18 at 15:02





@DJC Right, the difference is that the function that you pass to Observable.create is only called when the Observable is subscribed. Contrast this with the behavior of fromPromise, with which the supplied function is called when the Observable is created. reactivex.io/documentation/operators/create.html

– Myk Willis
Nov 14 '18 at 15:02













Perfect - thanks a lot for the detailed explanation. Turns out Angular University got it wrong ;)

– DJC
Nov 14 '18 at 15:10





Perfect - thanks a lot for the detailed explanation. Turns out Angular University got it wrong ;)

– DJC
Nov 14 '18 at 15:10













0














Stop using fetch and use HttpClient. If you open a console and type fetch('foo') you will see a net request. It's the invocation before fromPromise that's the issue.






share|improve this answer
























  • I'm following an Angular University course on rxjs - I don't intend to use fetch in any projects, but for this example I understand the http request shouldn't be happening without the subscription (according to the course author who hasn't yet come back to me). I'd really like to understand why this auto subscription is happening

    – DJC
    Nov 13 '18 at 19:39
















0














Stop using fetch and use HttpClient. If you open a console and type fetch('foo') you will see a net request. It's the invocation before fromPromise that's the issue.






share|improve this answer
























  • I'm following an Angular University course on rxjs - I don't intend to use fetch in any projects, but for this example I understand the http request shouldn't be happening without the subscription (according to the course author who hasn't yet come back to me). I'd really like to understand why this auto subscription is happening

    – DJC
    Nov 13 '18 at 19:39














0












0








0







Stop using fetch and use HttpClient. If you open a console and type fetch('foo') you will see a net request. It's the invocation before fromPromise that's the issue.






share|improve this answer













Stop using fetch and use HttpClient. If you open a console and type fetch('foo') you will see a net request. It's the invocation before fromPromise that's the issue.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 13 '18 at 19:23









bc1105bc1105

79846




79846













  • I'm following an Angular University course on rxjs - I don't intend to use fetch in any projects, but for this example I understand the http request shouldn't be happening without the subscription (according to the course author who hasn't yet come back to me). I'd really like to understand why this auto subscription is happening

    – DJC
    Nov 13 '18 at 19:39



















  • I'm following an Angular University course on rxjs - I don't intend to use fetch in any projects, but for this example I understand the http request shouldn't be happening without the subscription (according to the course author who hasn't yet come back to me). I'd really like to understand why this auto subscription is happening

    – DJC
    Nov 13 '18 at 19:39

















I'm following an Angular University course on rxjs - I don't intend to use fetch in any projects, but for this example I understand the http request shouldn't be happening without the subscription (according to the course author who hasn't yet come back to me). I'd really like to understand why this auto subscription is happening

– DJC
Nov 13 '18 at 19:39





I'm following an Angular University course on rxjs - I don't intend to use fetch in any projects, but for this example I understand the http request shouldn't be happening without the subscription (according to the course author who hasn't yet come back to me). I'd really like to understand why this auto subscription is happening

– DJC
Nov 13 '18 at 19:39


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53286573%2fhttp-request-within-observable-happens-even-without-subscription%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Xamarin.iOS Cant Deploy on Iphone

Glorious Revolution

Dulmage-Mendelsohn matrix decomposition in Python