title: ES6 async/await in classes
body: <p>I'm trying to create a class that will send a post request (login), save the cookie and use that cookie for other operations such as download a file.</p>
  
  <p>I created a local server that that will receive a post http method with user and password in it and a router called <code>/download</code> that will only be accessed if the user is logged in, otherwise it will return <code>you need to log in</code>.</p>
  
  <p>The problem:
  This is the prototype of my class (before hand):</p>
  
  <pre><code>const request = require('request-promise-native')
  
  class ImageDownloader {
    constructor(username = null, password = null) {
      this.username = username
      this.password = password
      this.cookie = request.jar()
  
      this.init()
    }
  
    init() {
      // login and get the cookie
    }
  
    download() {
      // needs the cookie
    }
  
    query() {
      // needs the cookie
    }
  }
  </code></pre>
  
  <p>As you can see in the code above I need the cookie for two operations that is <code>download</code> and <code>query</code> so I though about creating an <code>init</code> method that will do the initial operations such as login and call it right in the constructor so it will be initialized and put the cookie on the variable <code>this.cookie</code> to use everywhere, but it doesn't work, it seems that <code>init</code> is being called after every other method.</p>
  
  <pre><code>const request = require('request-promise-native')
  
  class ImageDownloader {
    constructor(username = null, password = null) {
      this.username = username
      this.password = password
      this.cookie = request.jar()
  
      this.init()
    }
  
    async init() {
      await request({
        uri: 'http://localhost/login',
        jar: this.cookie,
        method: 'post',
        formData: {
          'username': 'admin',
          'password': 'admin'
        }
      }).catch(e =&gt; console.error(e))
    }
  
    async download() {
      await request({
        uri: 'http://localhost/download/image.jpg',
        jar: this.cookie
      })
      .then(b =&gt; console.log(b))
      .catch(e =&gt; console.error(e))
    }
  
    query() {
      // ...
    }
  }
  
  const downloader = new ImageDownloader
  downloader.download()
  </code></pre>
  
  <p><strong>It's returning to me that I need to log in (server response)</strong>... BUT it works if I do this change:</p>
  
  <pre><code>async download() {
    await init() // &lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;
    await request({
      uri: 'http://localhost/download/image.jpg',
      jar: this.cookie
    })
    .then(b =&gt; console.log(b))
    .catch(e =&gt; console.error(e))
  }
  </code></pre>
  
  <p>It only works if I call <code>init</code> in the <code>download</code> method.</p>
  
  <p>If I put <code>console.log(this.cookie)</code> in <code>download</code> it returns an empty <code>CookieJar</code> and if I put the same in <code>init</code> it will return the right cookie but it appears <strong>AFTER</strong> the execution of download even tho I called it on the constructor before calling <code>download</code>.</p>
  
  <p>How to solve that? Thank you very much.</p>
  
  <p>@edit</p>
  
  <p>I made the changes that <strong>@agm1984</strong> and <strong>@Jaromanda X</strong> told me but it still doesn't work :(</p>
  
  <pre><code>const request = require('request-promise-native')
  
  class ImageDownloader {
    constructor(username = null, password = null) {
      this.username = username
      this.password = password
      this.cookie = request.jar()
  
      this.init().catch(e =&gt; console.error(e))
    }
  
    async init() {
      return await request({
        uri: 'http://localhost/login',
        jar: this.cookie,
        method: 'post',
        formData: {
          'username': 'admin',
          'password': 'admin'
        }
      })
    }
  
    async download() {
      return await request({
        uri: 'http://localhost/download/image.jpg',
        jar: this.cookie
      })
    }
  
    query() {
      // ...
    }
  }
  
  const downloader = new ImageDownloader
  downloader.download()
            .then(b =&gt; console.log(b))
            .catch(e =&gt; console.error(e))
  </code></pre>
  
  <p>But then again... it doesn't work unless I call <code>init</code> inside <code>download</code>.</p>