My NodeJS controller is sending the response before finish running the wait methods.
The response with the 'OK' message is immediately send back to the client (browser) when I run it. It sends the response before running all the methods.
try {
   const ep = await new keysModel().getKeys(); // key is a list of objects
   await ep.map( async (key) => {
      // some operations with the keys here
      // call a fetch to a third party API
      await fetch(urlRequest, {
         method: 'post',
         body:    JSON.stringify(myJSONObject),
         headers: { 
            'Authorization': `Basic ${base64.encode(`${key.APIKey}:${key.PasswordKey}`)}`,
            'Content-Type': 'application/json'
         },
      })
      .then(async res => res.json())
      .then(async json => {
         // Here I have some operations with the json response data
         // It includes some database queries
      })
      .catch(async err => {
         // await method to send me an email
         res.status(501).send('error');
      });
   });
   res.status(202).send('OK');
} catch() {
   // await method to send me an email
   res.status(501).send('error');
}
Below is the route:
const express =  require('express');
const router = express.Router();
router.get('/myPath', myController.testMethod);
I'm new with NodeJS. Trying to understand how it works.
Thanks
 
     
    