title: How to combine arguments from separate Promises?
body: <p>This problem should be clear: I want to write into a log file using a statement like the following. You can assume that this statement is inside a click handler for a button. There are two questions embedded in this pseudocode.</p>
  
  <pre><code>pLogInfo("local info").then(pauseUntilSettled).catch(err); // This is my goal
  </code></pre>
  
  <p>Here are my library functions (each returns a Promise):</p>
  
  <pre><code>// Get info asynchronously from a server (simple, should work)
  function pGetServerInfo()
      {
      // then value: "server info"
      } // pGetServerInfo
  
  // Write into a file asynchronously (simple, should work)
  function pWriteFile(path,string)
      {
      // no then value
      } // pWriteFile
  
  // Write formatted info into a log file asynchronously
  function pLogInfo(localInfo)
      {
      pGetServerInfo().then(p2);
      } // pLogInfo
  
  function p2(serverInfo)
      {
      // Should write "local info, server info"
      // But where is the local info? It got lost.
      return pWriteFile('log',localInfo+','+serverInfo);
      } // p2
  </code></pre>
  
  <p>Usage:</p>
  
  <pre><code>pLogInfo("local info").then(pauseUntilSettled).catch(err);
  
  function pauseUntilSettled()
      {
      // How to wait before returning from
      // the button click event handler?
      } // pauseUntilSettled
  </code></pre>
  
  <p>ADDED 8/27/19:</p>
  
  <p>Several possible solutions to this common problem occur to me:</p>
  
  <ol>
  <li><p>Attach an object to one of the top functions in your chain (p.data={}). You can store any arguments or callbacks you wish in the object and reference them in any asynchronous 'then' code. This works because the function that is the parent of the object has global scope. It can fail if you fire another top-level Promise of the same thread while an existing Promise is not yet settled, because the new execution thread will share and overwrite the object. I have successfully used this approach, a variant of the above global formulation, but it is clearly unsafe.</p></li>
  <li><p>Create a closure function to propagate the asynchronous thread. A closure function contains a snapshot of its arguments and referenced global variables. I have not yet gotten this idea to work, but it seems reasonable.</p></li>
  <li><p>Create a new Promise, either as part of the thread of Promises or as a separate helper, that calls its resolve function with an object instead of a single value. Use that object to propagate more than one value to each "then" function. I have not gotten this idea to work either.</p></li>
  </ol>
  
  <p>I hope these ideas inspires someone (including myself) to come up with a solution, because it is a very common problem that is not often discussed.</p>