title: JavaScript perform action before closing tab
body: <p>I have some problems performing some actions before client closes tab or browser. I'm using Vue, and I would need to send HTTP request to my backend to perform some action.
  I know that Vue has function <code>beforeDestroy()</code> and I have it implemented, but it can't perform that action if client closes tab or browser.
  I tried to do something like:</p>
  <pre><code>created() {
          window.addEventListener('beforeunload', this.leaveOrder())
      },
  </code></pre>
  <p>But still no success.
  Does anyone have any idea how could I fix that problem?</p>
  <p>btw: leaveOrder function is simple post request that looks like:</p>
  <pre><code>leaveOrder () {
              return new Promise((resolve, reject) =&gt; {
                  this.$http.post(&quot;/order/&quot; + this.order.id + '/leave').then(response =&gt; {
                          console.log('Leaving')
                      })
                      .catch(error =&gt; {
                          console.log('Something bad happeneds')
                      });
                  resolve(true)
              })
          },
  </code></pre>