1

There's many questions and solutions in SO but none of them work when called from componenent init which is not async. Here's an example:

  private delay(ms: number)
  {
    return new Promise(resolve => setTimeout(resolve, ms));
  }

  private async sleepExample()
  {
    console.log("Beforep: " + new Date().toString());
    // Sleep thread for 3 seconds
    await this.delay(3000);
    console.log("Afterp:  " + new Date().toString());
  }


  ngOnInit(): void {  
    console.log('ngOnInit');
    this.sleepExample();
    console.log('After sleep');
    // do lots of stuff
    console.log('After lots of stuff);
  }

The console output looks like this:

ngOnInit
Beforep: Mon Mar 18 2019 21:22:58 GMT+0200 (Eastern European Standard Time)
After sleep
…
After lots of stuff
Afterp:  Mon Mar 18 2019 21:23:02 GMT+0200 (Eastern European Standard Time)

How to sleep in ngOnInit? The reason I'm asking is that I'm trying to prototype a multiple window application that communicates with localStorage. 1st window started becomes main window. When I start 2 windows at almost same time the latter won't yet find the 1st window's stuff in localStorage and thinks it is also mainWindow. So logical solution would be to wait for 1-2 secs and try to read the localStorage again if the window didn't find anything. After "sleep" the 2nd window would find out that there actually was already a window and it becomes childwindow.

char m
  • 7,840
  • 14
  • 68
  • 117
  • You should not sleep in synchronous methods. What are you trying to do? – Bergi Mar 18 '19 at 19:33
  • 2
    Why would you need to? Specifying a use-case may help get an answer that will help you. – Matt Tester Mar 18 '19 at 19:33
  • Agreeing with Matt Tester, in order to get a good answer, you'll need to make your intended Use Case more clear. There are ways to make a component wait for the view to load completely, if that's what you are after (see ngAfterViewChecked, for one example) – J E Carter II Mar 18 '19 at 19:49
  • @Bergi and others: I want to wait for something that will be in localStorage in a bit. I edit my question. – char m Mar 18 '19 at 20:10
  • Not sure how these windows are created and if they are even components but if they are angular components perhaps it would be best to have them communicate to each other using events (see also Output and EventEmitter) – Igor Mar 18 '19 at 20:22
  • @Igor : different windows are different app instances so they can't communicate within the app as you mentioned. they can communicate either with websockets or localstorage. – char m Mar 18 '19 at 20:31

1 Answers1

1

The call in ngOnInit does not wait for the result from the asynchronous operation. You need to either run the code after the call inside a call back function passed to then or make ngOnInit asynchronous as well.

ngOnInit(): void {  
    console.log('ngOnInit');
    this.sleepExample().then(() => console.log('After sleep'));
}

or

async ngOnInit() {  
    console.log('ngOnInit');
    await this.sleepExample();
    console.log('After sleep');
}

As a side note you are using the wrong terminology. You can't "sleep" a thread in javascript nor should you want to. I would recommend reading through this previous question's answer as it is closely (if not directly) related to your question: How do I return the response from an asynchronous call?

Igor
  • 60,821
  • 10
  • 100
  • 175
  • thanks for fast answer! that's why is used quotations cause it does not exist. which is absolutely mind blowing. – char m Mar 18 '19 at 19:49
  • 4
    @charm - Javascript only has a single thread that executes. If you were to be able to sleep it no javascript code could run. It is not available because it is not needed or wanted. – Igor Mar 18 '19 at 19:50
  • the latter does not work. it does some redirecting. so "sleep" is possible only in busy loop? – char m Mar 18 '19 at 20:03
  • ok, i read more about it. it seems that only way to halt the entire program is to busy wait. – char m Mar 18 '19 at 20:08