title: NodeJS joining promise with request functions
body: <p>I have the following code below for clover payment api and although the code works perfectly, I do not get a response back when I call the api from postman. I know this is because of the multiple requests in the service and I tried to find a cleaner way to do it but keep failing to get it to work. I am trying to send back the final response which is the response of the request in the postPayment() function. Any help would be appreciated.</p>
  <p>my service code is:</p>
  <pre><code>const db = require('../_helpers/db');
  const crypto = require('crypto');
  const request = require(&quot;request-promise&quot;);
  
  
  module.exports = {
      getAll
  };
  
  var targetEnv = 'https://sandbox.dev.clover.com/v2/merchant/';
  var cardNumber = '6011361000006668';
  
  async function getAll(data) {
    var url = targetEnv + data.merchant_id + '/pay/key';
  
    var options = {
      url: url,
      method: 'GET',
      headers: {
        Authorization: 'Bearer ' + data.api_token
      }
    };
  
    request(options, (error, response, body) =&gt; {
      if (!error &amp;&amp; response.statusCode === 200) {
        console.log('getAll ' +data);
        processEncryption(JSON.parse(body), JSON.stringify(data));
      }
    });
  }
  
  // Process the encryption information received by the pay endpoint.
  function processEncryption(jsonResponse, data) {
    console.log('processEncryption ' +data);
    var prefix = jsonResponse['prefix'];
    var pem = jsonResponse['pem'];
  
    // create a cipher from the RSA key and use it to encrypt the card number, prepended with the prefix from GET /v2/merchant/{mId}/pay/key
    var encrypted = crypto.publicEncrypt(pem, Buffer(prefix + cardNumber));
  
    // Base64 encode the resulting encrypted data into a string to Clover as the 'cardEncrypted' property.
    var cardEncrypted = new Buffer(encrypted).toString('base64');
  
    return postPayment(cardEncrypted, data);
  }
  
  // Post the payment to the pay endpoint with the encrypted card information.
  async function postPayment(cardEncrypted, body) {
    // POST to /v2/merchant/{mId}/pay
    console.log('mid ' +JSON.parse(body));
    var posturl = targetEnv + '9ZQTAJSQKZ391/pay';
    var postData = {
      &quot;orderId&quot;: &quot;4N3RBF33EBEGT&quot;,
      &quot;currency&quot;: &quot;usd&quot;,
      &quot;amount&quot;: 2,
      &quot;tipAmount&quot;: 0,
      &quot;taxAmount&quot;: 0,
      &quot;expMonth&quot;: 12,
      &quot;cvv&quot;: 123,
      &quot;expYear&quot;: 2018,
      &quot;cardEncrypted&quot;: cardEncrypted,
      &quot;last4&quot;: 6668,
      &quot;first6&quot;: 601136,
      &quot;streetAddress&quot;: &quot;123 Fake street&quot;,
      &quot;zip&quot;: &quot;94080&quot;,
      &quot;merchant_id&quot;: &quot;9ZQTAJSQKZ391&quot;,
      &quot;order_id&quot;: &quot;4N3RBF33EBEGT&quot;,
      &quot;api_token&quot;: &quot;4792a281-38a9-868d-b33d-e36ecbad66f5&quot;
    }
  
    var options = {
      url: posturl,
      method: 'POST',
      headers: {
        'Authorization': 'Bearer ' + &quot;4792a281-38a9-868d-b33d-e36ecbad66f5&quot;,
      },
      json: postData
    };
  
     request(options, (error, response, body) =&gt; {
      if (!error &amp;&amp; response.statusCode === 200) {
        //console.log(response);  
        return response;   &lt;---- this response is what i need to show in postman
      }
    });
     console.log(response);
  }
  </code></pre>
  <p>my controller is:</p>
  <pre><code>const express = require('express');
  const router = express.Router();
  const tableOrderService = require('./cloverPayment.service');
  
  // routes
  router.post('/getAll', getAll);
  
  
  module.exports = router;
  
  
  function getAll(req, res, next) {
      tableOrderService.getAll(req.body)
          .then(users =&gt; res.json(users))
          .catch(err =&gt; next(err));
  }
  </code></pre>