Wait until Angular Observable is “Complete”





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















Looked around and nothing I found quite fit my problem. How can I make the rest of .ts code to wait for an observable to finish.



I'm new to Angular and still trying to figure out how Observables work. I'm familiar with using them in my templates with '| async' but using them to get data in my .ts files isn't working how I expected.



I want everything to wait for the Observable to complete before moving on. My goal here is to set the class variable 'briefed' based on the response of my observable.



I'm going to be using that variable



Unfortunately, the briefing variable never changes. I'm positive this is a fundamental misunderstanding on my part.



My class essentially boils down to:



export class briefValidatorComponent implements OnInit{
briefed: boolean = false;
output: customObjectTwo;

@Input() inputOne: customObject;
@Input() inputTwo: customObject;

ngOnIt(){
this.userService
.getBriefingReflectedForUser()
.subscribe(response => {
this.briefed = response.briefingInCasport; //Returns true
console.log(this.briefed); //Prints true
});

console.log(this.briefed); //Prints false

functionOne(objectOne, objectTwo);
}

functionOne(objectOne, objectTwo){
//does some work with the briefed variable to
//determine what the output will be
}


EDIT: Sorry, should have been more complete with my intent. Haven't written the rest of the code yet because I got stumped here.



I'll have another function that evaluate some input data to determine what the output will be. I'll then reference that output in my dom to change what will be shown there.



I looked into doing all the work in the subscribe method but I wouldn't be able to then change the output for the dom to reference.










share|improve this question




















  • 2





    You don't wait. You need to deal with the fact that the data may not initially be available, or use a resolver to hold the component loading until it is.

    – jonrsharpe
    Nov 16 '18 at 20:41






  • 2





    the fact that you're asking this question actually shows you understand the concept perfectly. this is how asynchronous functions work in javascript. that said, you can use native asyncawait functions to get the behavior you're after. but if using angular i'd stick with observables.

    – Stavm
    Nov 16 '18 at 20:51








  • 1





    I want everything to wait for the Observable to complete before moving on. If grandma has to wait until your Observable completes to put a cup of tea on we're going to have problems. No, but seriously do you want something in your component template (HTML) to wait to load or do you want some other logical code in your component to wait, can you be more specific than "everything?"

    – Narm
    Nov 16 '18 at 22:57













  • Thanks for the responses! I edited my post to hopefully be more clear with my intent. To briefly sum it up. I want to be able to do work to change a variable that will be used to determine what is shown in the dom.

    – OvaltineJenkins
    Nov 17 '18 at 1:30











  • Subscription is kind of callback, the whole idea it doesn't block. You'll have to call functionOne() inside subscription block or use it's third param: .subscribe(response => console.log(response), errors => {}, () => this.functionOne()). There's (learnrxjs.io/operators/utility/finalize.html) finalize operator for pipe is available as well which works the same

    – WildDev
    Nov 17 '18 at 8:17




















0















Looked around and nothing I found quite fit my problem. How can I make the rest of .ts code to wait for an observable to finish.



I'm new to Angular and still trying to figure out how Observables work. I'm familiar with using them in my templates with '| async' but using them to get data in my .ts files isn't working how I expected.



I want everything to wait for the Observable to complete before moving on. My goal here is to set the class variable 'briefed' based on the response of my observable.



I'm going to be using that variable



Unfortunately, the briefing variable never changes. I'm positive this is a fundamental misunderstanding on my part.



My class essentially boils down to:



export class briefValidatorComponent implements OnInit{
briefed: boolean = false;
output: customObjectTwo;

@Input() inputOne: customObject;
@Input() inputTwo: customObject;

ngOnIt(){
this.userService
.getBriefingReflectedForUser()
.subscribe(response => {
this.briefed = response.briefingInCasport; //Returns true
console.log(this.briefed); //Prints true
});

console.log(this.briefed); //Prints false

functionOne(objectOne, objectTwo);
}

functionOne(objectOne, objectTwo){
//does some work with the briefed variable to
//determine what the output will be
}


EDIT: Sorry, should have been more complete with my intent. Haven't written the rest of the code yet because I got stumped here.



I'll have another function that evaluate some input data to determine what the output will be. I'll then reference that output in my dom to change what will be shown there.



I looked into doing all the work in the subscribe method but I wouldn't be able to then change the output for the dom to reference.










share|improve this question




















  • 2





    You don't wait. You need to deal with the fact that the data may not initially be available, or use a resolver to hold the component loading until it is.

    – jonrsharpe
    Nov 16 '18 at 20:41






  • 2





    the fact that you're asking this question actually shows you understand the concept perfectly. this is how asynchronous functions work in javascript. that said, you can use native asyncawait functions to get the behavior you're after. but if using angular i'd stick with observables.

    – Stavm
    Nov 16 '18 at 20:51








  • 1





    I want everything to wait for the Observable to complete before moving on. If grandma has to wait until your Observable completes to put a cup of tea on we're going to have problems. No, but seriously do you want something in your component template (HTML) to wait to load or do you want some other logical code in your component to wait, can you be more specific than "everything?"

    – Narm
    Nov 16 '18 at 22:57













  • Thanks for the responses! I edited my post to hopefully be more clear with my intent. To briefly sum it up. I want to be able to do work to change a variable that will be used to determine what is shown in the dom.

    – OvaltineJenkins
    Nov 17 '18 at 1:30











  • Subscription is kind of callback, the whole idea it doesn't block. You'll have to call functionOne() inside subscription block or use it's third param: .subscribe(response => console.log(response), errors => {}, () => this.functionOne()). There's (learnrxjs.io/operators/utility/finalize.html) finalize operator for pipe is available as well which works the same

    – WildDev
    Nov 17 '18 at 8:17
















0












0








0








Looked around and nothing I found quite fit my problem. How can I make the rest of .ts code to wait for an observable to finish.



I'm new to Angular and still trying to figure out how Observables work. I'm familiar with using them in my templates with '| async' but using them to get data in my .ts files isn't working how I expected.



I want everything to wait for the Observable to complete before moving on. My goal here is to set the class variable 'briefed' based on the response of my observable.



I'm going to be using that variable



Unfortunately, the briefing variable never changes. I'm positive this is a fundamental misunderstanding on my part.



My class essentially boils down to:



export class briefValidatorComponent implements OnInit{
briefed: boolean = false;
output: customObjectTwo;

@Input() inputOne: customObject;
@Input() inputTwo: customObject;

ngOnIt(){
this.userService
.getBriefingReflectedForUser()
.subscribe(response => {
this.briefed = response.briefingInCasport; //Returns true
console.log(this.briefed); //Prints true
});

console.log(this.briefed); //Prints false

functionOne(objectOne, objectTwo);
}

functionOne(objectOne, objectTwo){
//does some work with the briefed variable to
//determine what the output will be
}


EDIT: Sorry, should have been more complete with my intent. Haven't written the rest of the code yet because I got stumped here.



I'll have another function that evaluate some input data to determine what the output will be. I'll then reference that output in my dom to change what will be shown there.



I looked into doing all the work in the subscribe method but I wouldn't be able to then change the output for the dom to reference.










share|improve this question
















Looked around and nothing I found quite fit my problem. How can I make the rest of .ts code to wait for an observable to finish.



I'm new to Angular and still trying to figure out how Observables work. I'm familiar with using them in my templates with '| async' but using them to get data in my .ts files isn't working how I expected.



I want everything to wait for the Observable to complete before moving on. My goal here is to set the class variable 'briefed' based on the response of my observable.



I'm going to be using that variable



Unfortunately, the briefing variable never changes. I'm positive this is a fundamental misunderstanding on my part.



My class essentially boils down to:



export class briefValidatorComponent implements OnInit{
briefed: boolean = false;
output: customObjectTwo;

@Input() inputOne: customObject;
@Input() inputTwo: customObject;

ngOnIt(){
this.userService
.getBriefingReflectedForUser()
.subscribe(response => {
this.briefed = response.briefingInCasport; //Returns true
console.log(this.briefed); //Prints true
});

console.log(this.briefed); //Prints false

functionOne(objectOne, objectTwo);
}

functionOne(objectOne, objectTwo){
//does some work with the briefed variable to
//determine what the output will be
}


EDIT: Sorry, should have been more complete with my intent. Haven't written the rest of the code yet because I got stumped here.



I'll have another function that evaluate some input data to determine what the output will be. I'll then reference that output in my dom to change what will be shown there.



I looked into doing all the work in the subscribe method but I wouldn't be able to then change the output for the dom to reference.







angular rxjs observable






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 17 '18 at 1:28







OvaltineJenkins

















asked Nov 16 '18 at 20:38









OvaltineJenkinsOvaltineJenkins

215




215








  • 2





    You don't wait. You need to deal with the fact that the data may not initially be available, or use a resolver to hold the component loading until it is.

    – jonrsharpe
    Nov 16 '18 at 20:41






  • 2





    the fact that you're asking this question actually shows you understand the concept perfectly. this is how asynchronous functions work in javascript. that said, you can use native asyncawait functions to get the behavior you're after. but if using angular i'd stick with observables.

    – Stavm
    Nov 16 '18 at 20:51








  • 1





    I want everything to wait for the Observable to complete before moving on. If grandma has to wait until your Observable completes to put a cup of tea on we're going to have problems. No, but seriously do you want something in your component template (HTML) to wait to load or do you want some other logical code in your component to wait, can you be more specific than "everything?"

    – Narm
    Nov 16 '18 at 22:57













  • Thanks for the responses! I edited my post to hopefully be more clear with my intent. To briefly sum it up. I want to be able to do work to change a variable that will be used to determine what is shown in the dom.

    – OvaltineJenkins
    Nov 17 '18 at 1:30











  • Subscription is kind of callback, the whole idea it doesn't block. You'll have to call functionOne() inside subscription block or use it's third param: .subscribe(response => console.log(response), errors => {}, () => this.functionOne()). There's (learnrxjs.io/operators/utility/finalize.html) finalize operator for pipe is available as well which works the same

    – WildDev
    Nov 17 '18 at 8:17
















  • 2





    You don't wait. You need to deal with the fact that the data may not initially be available, or use a resolver to hold the component loading until it is.

    – jonrsharpe
    Nov 16 '18 at 20:41






  • 2





    the fact that you're asking this question actually shows you understand the concept perfectly. this is how asynchronous functions work in javascript. that said, you can use native asyncawait functions to get the behavior you're after. but if using angular i'd stick with observables.

    – Stavm
    Nov 16 '18 at 20:51








  • 1





    I want everything to wait for the Observable to complete before moving on. If grandma has to wait until your Observable completes to put a cup of tea on we're going to have problems. No, but seriously do you want something in your component template (HTML) to wait to load or do you want some other logical code in your component to wait, can you be more specific than "everything?"

    – Narm
    Nov 16 '18 at 22:57













  • Thanks for the responses! I edited my post to hopefully be more clear with my intent. To briefly sum it up. I want to be able to do work to change a variable that will be used to determine what is shown in the dom.

    – OvaltineJenkins
    Nov 17 '18 at 1:30











  • Subscription is kind of callback, the whole idea it doesn't block. You'll have to call functionOne() inside subscription block or use it's third param: .subscribe(response => console.log(response), errors => {}, () => this.functionOne()). There's (learnrxjs.io/operators/utility/finalize.html) finalize operator for pipe is available as well which works the same

    – WildDev
    Nov 17 '18 at 8:17










2




2





You don't wait. You need to deal with the fact that the data may not initially be available, or use a resolver to hold the component loading until it is.

– jonrsharpe
Nov 16 '18 at 20:41





You don't wait. You need to deal with the fact that the data may not initially be available, or use a resolver to hold the component loading until it is.

– jonrsharpe
Nov 16 '18 at 20:41




2




2





the fact that you're asking this question actually shows you understand the concept perfectly. this is how asynchronous functions work in javascript. that said, you can use native asyncawait functions to get the behavior you're after. but if using angular i'd stick with observables.

– Stavm
Nov 16 '18 at 20:51







the fact that you're asking this question actually shows you understand the concept perfectly. this is how asynchronous functions work in javascript. that said, you can use native asyncawait functions to get the behavior you're after. but if using angular i'd stick with observables.

– Stavm
Nov 16 '18 at 20:51






1




1





I want everything to wait for the Observable to complete before moving on. If grandma has to wait until your Observable completes to put a cup of tea on we're going to have problems. No, but seriously do you want something in your component template (HTML) to wait to load or do you want some other logical code in your component to wait, can you be more specific than "everything?"

– Narm
Nov 16 '18 at 22:57







I want everything to wait for the Observable to complete before moving on. If grandma has to wait until your Observable completes to put a cup of tea on we're going to have problems. No, but seriously do you want something in your component template (HTML) to wait to load or do you want some other logical code in your component to wait, can you be more specific than "everything?"

– Narm
Nov 16 '18 at 22:57















Thanks for the responses! I edited my post to hopefully be more clear with my intent. To briefly sum it up. I want to be able to do work to change a variable that will be used to determine what is shown in the dom.

– OvaltineJenkins
Nov 17 '18 at 1:30





Thanks for the responses! I edited my post to hopefully be more clear with my intent. To briefly sum it up. I want to be able to do work to change a variable that will be used to determine what is shown in the dom.

– OvaltineJenkins
Nov 17 '18 at 1:30













Subscription is kind of callback, the whole idea it doesn't block. You'll have to call functionOne() inside subscription block or use it's third param: .subscribe(response => console.log(response), errors => {}, () => this.functionOne()). There's (learnrxjs.io/operators/utility/finalize.html) finalize operator for pipe is available as well which works the same

– WildDev
Nov 17 '18 at 8:17







Subscription is kind of callback, the whole idea it doesn't block. You'll have to call functionOne() inside subscription block or use it's third param: .subscribe(response => console.log(response), errors => {}, () => this.functionOne()). There's (learnrxjs.io/operators/utility/finalize.html) finalize operator for pipe is available as well which works the same

– WildDev
Nov 17 '18 at 8:17














0






active

oldest

votes












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%2f53345059%2fwait-until-angular-observable-is-complete%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















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%2f53345059%2fwait-until-angular-observable-is-complete%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

Bressuire

Vorschmack

Quarantine