Issue with 2 Promises(await) under single async function

  api, axios, javascript, node-fetch, node.js

Let’s say I have a module with 2 Promises under single module(function) like this:

exports.get_info = async(req, res) => 
{
  const first_post_call = await axios.post(url, data_to_be_submitted)
  token = first_post_call.data.token

  const second_post_call await.post(url, token)
  console.log(second_post_call.data)
}

Above mostly is not working in my case(strangely). But when I split them into 2 modules with same exact code like that:(it works perfectly)

exports.first_call = async(req, res) => 
{
  const first_post_call = await axios.post(url, data_to_be_submitted)
  return res.status(200).json.({token: first_post_call.data.token})
}

Btw I am using Postman, I make the above call and retrieve the token in response in order to pass it in second_call module’s body:

exports.second_call= async(req, res) => 
{
  const second_post_call = await axios.post(url, req.body.token)
  return res.status(200).json.({data: second_post_call.data})
}

So, It is working when Promises are in separate functions only.
Please tell what could possibly be an issue in first case. Because eventually I have to implement both Promises under single module.
I been trying to figure out what the heck is wrong with my code for last 3-4 days, I have no clue.
My full code is here: 2 Awaits are not working under single async, but both awaits working in separate modules
Btw, I have tried above with both Axios and Node-fetch modules

Source: Ask Javascript Questions

LEAVE A COMMENT