title: Angular 4 - Can OnDestroy be run without the OnInit ever being triggered?
body: <p>Just a generic question regarding the two Lifecycle Hooks <code>OnInit</code> and <code>OnDestroy</code>. As this <a href="https://www.bennadel.com/blog/3356-ngoninit-may-not-get-called-before-ngondestroy-is-called-in-angular-4-4-6.htm" rel="noreferrer">article</a> mentions, I had always assumed that <code>OnInit</code> is  <strong>always</strong> run before the <code>OnDestroy</code>.</p>
  
  <p>I have a case where in an <code>ngOnDestroy()</code> I'm stopping a background music track from playing. This sound is loaded the component's <code>ngOnInit()</code>, and because the <code>ngOnDestroy()</code> is being run without the <code>ngOnInit()</code> being run the sound object is <code>undefined</code>.</p>
  
  <p><strong>Code</strong></p>
  
  <pre><code>ngOnInit() {
      ...
  
      this.loadSounds();
  
      ...
  }
  
  ngOnDestroy() {
      if (AppSettings.SOUNDS_ENABLED) {
          this.soundService.getSound(Sound.MINI_GAME_BG_MUSIC).fade(0.2, 0, 1500);
      }
  }
  
  private loadSounds() {
      this.soundService.loadSound(Sound.MINI_GAME_BG_MUSIC, SoundPathURL.MINI_GAME_BG_MUSIC, true, 0);
  }
  </code></pre>
  
  <p>At the moment the code is failing in the <code>ngOnDestroy</code> when trying to <code>.fade()</code> a sound that's <code>undefined</code>. Of course I can easily fix this by checking that the sound is not <code>undefined</code> before executing the <code>.fade()</code> function. My assumption was that if an <code>ngOnDestroy()</code> is run the <code>ngOnInit()</code> must have been run as well - I guess I was wrong.</p>
  
  <p>Now because of this case I'm thinking that in every <code>ngOnDestroy()</code> in my application I should check whether the object being used is <code>undefined</code> before performing any operations. So for example before unsubscribing from a subscription I should check if the subscription is <code>undefined</code>, and so on.</p>
  
  <p>Am I right to assume so? </p>