title: Test an Async function in JS - Error: "Did you forget to use await"
body: <p>My code looks like this: </p>
  
  <pre><code>public getUrl(url) {
  //returns URL
   ... }
  
  public getResponseFromURL(): container {
    let myStatus = 4;
    const abc = http.get(url, (respon) =&gt; 
    const { statusCode } = respon;
    myStatus = statusCode;
    console.log('Inside callback' +myStatus);
    .on('error', (err) =&gt; {
    console.log('Things have gone wrong' + err);
    });
    console.log('ITS COMPLICATED' +myStatus);
    return new Container(status, body, header);
    }
  }
  </code></pre>
  
  <p>The problem I am facing is because of the asynchronous nature of JS and the console.log('ITS COMPLICATED') gets executed before the one in the callback function. I am trying to have the first one executed before the last console.log!</p>
  
  <p>I am using Async/Await like below: </p>
  
  <pre><code>  public timeoutPromise(time: any) {
      return new Promise((resolve) =&gt; {
        setTimeout(() =&gt; {
          resolve(Date.now());
        }, time);
      });
    }
  
    public doSomethingAsync() {
      return this.timeoutPromise(1000);
    }
  </code></pre>
  
  <p>As a result changed my getResponseFromURL() to: </p>
  
  <pre><code>public async getResponseFromURL(): Promise&lt;container&gt; {
      this.myStatus = 7;
      console.log(0);
      await this.doSomethingAsync();
      console.log(1);
      const abc = http.get(url, (respon) =&gt; {
        const { statusCode } = respon;
        this.myStatus = statusCode;
        console.log('Inside Callback ' + statusCode);
      }).on('error', (err) =&gt; {
        console.log('Things have gone wrong ' + err);
      });
      await this.doSomethingAsync();
      console.log(2);
      await this.doSomethingAsync();
      console.log('Is it simple lalala ' + this.myStatus);
      await this.doSomethingAsync();
     }
  }
  </code></pre>
  
  <p>The problem with doing this was if my container class (return type of getResponseFromURL()) is a container for status and body when I am testing this async function, before <code>expect.getResponseFromURL().getStatus().toBe(200)</code> would work.</p>
  
  <p>Test looks like below: </p>
  
  <pre><code>  test('Async function', async () =&gt; {
      expect.assertions(1);
      const data = await ContainerGlobals.getResponseFromURL().getStatus();
      expect(data).toBe(207);
    });
  </code></pre>
  
  <p>Now I am getting error from <code>.getStatus()</code> and I am not sure how to bypass this error? </p>
  
  <blockquote>
    <p>"does not exist on Promise" </p>
  </blockquote>