0

So I implemented a canDeactivate function which expects an observable of type boolean to be returned. Condition to leave is async so I can't do something like this for example:

public canDeactivate(): Observable<boolean> {
    setTimeout(() => {
        return of(true);
    }, 5000);
}

How can I make a request inside this function that will determine if I can leave or not?

3 Answers 3

2

Use native rxjs for that, no need for timeouts

public canDeactivate(): Observable<boolean> {
    return interval(5000).pipe(take(1),mapTo(true));
}

or

public canDeactivate(): Observable<boolean> {
  return of(true).pipe(delay(5000);
}
Sign up to request clarification or add additional context in comments.

Comments

1

You can do it like this.

public canDeactivate(): Observable<boolean> {
  return new Observable<boolean>( observer => {
    setTimeout( () => {
      observer.next(true);
    }, 5000);
  });
}

Comments

1

You can create an Observable instance and return it

return new Observable(obs=>{
 setTimeout(() => {
        return obs.next(true);
    }, 5000);
)}

https://rxjs.dev/guide/observable

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.