title: Waiting for a response from app.post in another app.post
body: <p>I'm developing a back-end for a mobile application using express.js for my API.</p>
  
  <p>For this mobile application, the users sign-in using mobile numbers, an OTP code is sent to their mobiles, and they need to send back the OTP they received to the server for verification and validation.</p>
  
  <p>When the users first attempt to sign-in, they POST their mobile number to the server, and then a bunch of processing happens, and an OTP is sent to them through an SMS gateway.</p>
  
  <p>Now while this request is still ongoing, I need to wait for the users to send the OTP through a POST request to another route, verify it, and then proceed on with the appropriate steps in the first, ongoing POST request.</p>
  
  <p>After some search on the net, I eventually decided to wrap the app.post method for the verifyOTP route in a function that creates and returns a new promise, and then resolve it or reject it after verification. This worked wonderfully for the first time I perform this operation after restarting the server, but that's it. It only works the first time, and then for the consecutive times that follow, none of the new promises that should be created are resolved or rejected, and the first request to the sign-in route remains waiting.</p>
  
  <p>I tried a bunch of things like making the function wrapping the verifyOTP route async, and creating promises inside the route instead of wrapping it in one, but still no use. Can you help me?</p>
  
  <p>For the sake of finding a solution for this problem, I've simplified the process and did a simulation of the actual situation using this code, and it simulates the problem well:</p>
  
  <p><strong>This is to simulate the first request:</strong></p>
  
  <pre><code>app.get("/test", async function(req, res) {
      console.log("Test route\n");
  
      var otpCode = Math.floor(Math.random() * (9999 - 2)) + 1;
      var timestamp = Date.now();
  
      otp = {
          code: otpCode,
          generated: timestamp
      };
  
      console.log("OTP code sent: " + otpCode + "\n");
      console.log("OTP sent.\n");
  
      res.end();
  
      /*  verifyOTP().then(function() {
              console.log("Resolved OTP verification\n\n");
              res.end();
          }).catch(function() {
              console.log("Bad\n\n");
              res.end();
          });*/
  });
  </code></pre>
  
  <p><strong>This is the verifyOTP route:</strong></p>
  
  <pre><code>var otp;
  
  app.post("/verifyOTP", function(req, res) {
      console.log("POST request - verify OTP request\n");
  
      var msg;
      if ((Date.now() - otp.generated) / 1000 &gt; 30) {
          msg = "OTP code is no longer valid.";
          res.status(403).json({
              error: msg
          });
      } else {
          var submitted = req.body.otp;
          if (submitted !== otp.code) {
              msg = "OTP code is incorrect.";
              res.status(403).json({
                  error: msg
              });
          } else {
              msg = "Verified.";
              res.end();
          }
      }
      console.log(res.statusCode + " - " + res.statusMessage + "\n");
      console.log(msg + "\n");
  });
  </code></pre>
  
  <p>Just to mention, this isn't the only place in my server that I need OTP verification, although the implementation of what happens after the verification varies. Therefore, I'd appreciate it if the solution could still keep the code reusable for multiple instances..</p>