title: Returning 'resolve' function in Javascript Promise
body: <p>I have found a piece of code in <a href="https://spin.atomicobject.com/2018/09/10/javascript-concurrency/" rel="nofollow noreferrer">this blog</a> that works perfectly but makes a use of Promises that is hard to understand</p>
  
  <pre><code>export class Mutex {
    private mutex = Promise.resolve();
  
    lock(): PromiseLike&lt;() =&gt; void&gt; {
      let begin: (unlock: () =&gt; void) =&gt; void = unlock =&gt; {};
  
      this.mutex = this.mutex.then(() =&gt; {
        return new Promise(begin);
      });
  
      return new Promise(res =&gt; {
        begin = res;
      });
    }
  
    async dispatch(fn: (() =&gt; T) | (() =&gt; PromiseLike&lt;T&gt;)): Promise&lt;T&gt; {
      const unlock = await this.lock();
      try {
        return await Promise.resolve(fn());
      } finally {
        unlock();
      }
    }
  }
  </code></pre>
  
  <ul>
  <li><p>Is valid the expression <code>new Promise(res =&gt; { begin = res; })</code>? Promises usually involve calling <code>resolve</code> <em>on something</em></p></li>
  <li><p>Why does <code>const unlock = await this.lock();</code> resolve to a function?</p></li>
  </ul>