title: Communication between node.js app and worker on IronWorker. How to properly poll a task status and receive task response?
body: <p>The application is as follows:</p>
  
  <p>First of all, I`m using a node.js app with the express.js framework as web server. On the front-end, when the user sends a payment via a POST form, the express.js routes this task via 'app.post' and calls an external API for payment. This API request should ideally return the URL for which the user should be redirected for payment. Then, the express.js should redirect the user via the 'app.post' response variable (res): </p>
  
  <p><code>res.redirect(307, url);</code></p>
  
  <p>Since this payment API request takes some time and since I am using Heroku, the API request times out and an error is returned. To solve this, I know I would need to set a worker on an worker Heroku add-on (according to <a href="https://stackoverflow.com/a/11438381/2859410">https://stackoverflow.com/a/11438381/2859410</a>), such as IronWorker. However, I am having trouble to understand what would be the proper mechanism to communicate the URL returned by the external API request made in the worker back to the node.js app.</p>
  
  <p>To make things clearer:
  - The user submits the payment form.<br>
  - The node.js app processes the POST data and calls a worker ("enqueues" a task on a worker).<br>
  - The worker requests an external payment API, which returns an URL.<br>
  - The worker should communicate this URL back to the node.js app.<br>
  - The node.js app should receive this URL and redirect the user.<br>
  Yet, these operations have to be non-blocking.  </p>
  
  <p>I can think of 2 alternatives for the communication:<br>
  Alternative 1 - When the front-end user requests a payment via POST, the node.js app calls the specific worker (also via POST), with the payment data as the payload. The worker process the data, requests the payment API and gets the payment URL. Meanwhile, the node.js performs a polling of the worker status and, when receives the "completed" status, redirects the user.<br>
  Fallacy: The difficulty here for me is to perform constant polling in a asynchronous manner. Since I need to redirect the user to the payment URL, I would need to have the URL inside the "app.post('/form', function(req,res) {" method, such that I can the response ("res") to redirect the user.
  For this, I took a look at async.whilst(test, fn, callback), but I don`t know how could I use a test function that is synchronous. My immediate idea would be a test function that issues a status request and a callback function that redirects the user. However, this test function is an asynchronous operation (for example using 'request' module).</p>
  
  <p>Alternative 2 - The worker also receives the payment data as payload, process the data, requests the payment API and gets the payment URL. When it finishes, it communicates the URL back to the node.js app via an HTTP request.<br>
  Fallacy: How would I tie the URL communicated back to the node.js app to the user that requested the payment? In my understanding, the HTTP request with the payment URL would have an independent route than the form POST route. In another words, this way I would not be able to have the payment URL inside the "app.post('/orderform', function(req,res) {" method, and, therefore, would not be able to use response variable ("res") to redirect the user.</p>
  
  <p>Tha node.js app would be structured as follows:</p>
  
  <pre><code>app.post('/order', function(req, res) {  
      // user data on req.body
      // process data and request payment API  
      // Wait until URL is communicated to the endpoint belo '/url'
      // receive URL from the other endpoint and redirect user
  }
  
  app.post('/url', function(req, res) {  
      // receive payment URL on req.body
      // communicate the URL to the '/order/ endpoint    
  }
  </code></pre>
  
  <p>What would be recommended in this case?</p>
  
  <p>Thanks in advance.</p>