title: What is proper way to reuse socket between promise executor function invocations?
body: <p>I want to reuse the same socket across all <code>Promise</code> executor function invocations. However, this socket reuse introduces a new problem that once a response comes in from the sever, the right <code>Promise</code> must be <code>resolve()</code>d.</p>
  <p>So I came up with following design to <code>resolve()</code> only the right promise:</p>
  <pre class="lang-js prettyprint-override"><code>const dgram = require('dgram');
  
  const socket = dgram.createSocket('udp4');
  
  async function lookup(id) {
    socket.send(JSON.stringify({ &quot;id&quot;: id }), &quot;10000&quot;, &quot;127.0.0.1&quot;);
    let p = new Promise(function (resolve, reject) {
      socket.on(&quot;message&quot;, (msg, rinfo) =&gt; {
        let response = JSON.parse(msg)
        if (response.id === id) {// resolve only one response
          resolve(response.value);
        }
      })
    })
    let value = await p;
    console.log(`resolved ${id} to ${value}`)
  }
  
  lookup(&quot;1&quot;)
  lookup(&quot;2&quot;)
  </code></pre>
  <p>However, the design above still has shortcomings. On each incoming UDP message from the server the program will call the &quot;message&quot; event handler for each <code>Promise</code> that is still in pending state. This is quite inefficient if server went down for brief time or if the <code>lookup()</code> function was invoked in bursts.</p>
  <p>So I came up with second design below where the <code>resolve()</code> function is stored in a global <code>PromiseMap</code> and once the response comes in, the right <code>resolve()</code> function is called from a single handler:</p>
  <pre class="lang-js prettyprint-override"><code>const dgram = require('dgram');
  
  const socket = dgram.createSocket('udp4');
  
  var PromiseMap = {}
  
  async function lookup(id) {
    socket.send(JSON.stringify({&quot;id&quot;:id}), &quot;10000&quot;, &quot;127.0.0.1&quot;);
    let p = new Promise(function (resolve, reject) {
        PromiseMap[id] = resolve;
    })
    let value = await p;
    console.log(`resolved ${id} to ${value}`)
  }
  
  socket.on(&quot;message&quot;, (msg, rinfo) =&gt; {
    let response = JSON.parse(msg)
    if (response.id in PromiseMap) {
      PromiseMap[response.id](response.value);
    }
    delete PromiseMap[response.id]
  })
  
  lookup(&quot;1&quot;)
  lookup(&quot;2&quot;)
  </code></pre>
  <p>This seems to work. However, I am wondering what is generally accepted way to reuse sockets across <code>Promise</code>s?</p>
  <p>The protocol in my simplified example above looks like this:</p>
  <pre><code>nc -u -l 10000
  {&quot;id&quot;:&quot;1&quot;}{&quot;id&quot;:&quot;2&quot;} &lt;---two requests coming in from client
  {&quot;id&quot;:&quot;2&quot;, &quot;value&quot;:&quot;a&quot;} &lt;---Response two comes in first
  {&quot;id&quot;:&quot;1&quot;, &quot;value&quot;:&quot;b&quot;} &lt;--- only then response to first request comes in
  </code></pre>