Home > Net >  how to await value from another await variable
how to await value from another await variable

Time:09-02

I cannot figure out how should I construct my code.

Basic info:

webhook (intercom) --> google cloud functions (await values) --> post message to slack.

Issue:

My code is working fine until I need to get an value from another await function and I am not sure how should I 'pause' the second await until the first one is complete.

Code:

// first function to get the information about agent
const getTeammateInfo = async function (teammate_id) {
  try {
    const response = await axios.get("https://api.intercom.io/admins/"   teammate_id, {
      headers: {
        'Authorization': "Bearer "   INTERCOM_API_AUTH_TOKEN,
        'Content-type': "application/json",
        'Accept': "application/json"
      }
    });
    const { data } = response
    return data
  } catch (error) {
    console.error(error);
  }
};

// second function, which needs values from first function in order to find data
const slackID = async function (slack_email) {
  if (slackID) {
    try {
      const response = await axios.get("https://api.intercom.io/admins/"   slack_email, {
        headers: {
          'Authorization': "Bearer "   SLACK_API_TOKEN,
          'Content-type': "application/json",
          'Accept': "application/json"
        }
      });
      const { user } = response
      return user
    } catch (error) {
      console.error(error);
    }
  }
};

It is used within the Google Cloud Function (

exports.execute = async (req, res) => {
  try {

    // map of req from intercom webhook
    let {
      data: {
        item: {
          conversation_rating: {
            rating,
            remark,
            contact: {
              id: customerId
            },
            teammate: {
              id: teammateId
            }
          }
        }
      }

    } = req.body

  const teammateName = await getTeammateInfo(teammateId); // this works fine
  const slackTeammateId = await slackID(teammateName.email) // this is where it fails I need to get the values from 'teammateName' in order for the function slackID to work


  ...

 } catch (error) {
    console.error(error)
    res.status(500)
    res.json({ error: error })
  }

}

I have tried with Promise.all

const [teammateName, slackTeammateId] = await Promise.all([
  getTeammateInfo(teammateId), 
  slackID(teammateName.email)
])

But I just cannot wrap my head around this how it should work.

Thank you.

// edit:

Code is okay, I just put the wrong API into the slackID function... Thanks for double checking.

CodePudding user response:

The problem with Promise.all() is that it fires both functions at the same time and waits until both are done, so you can't chain that way.

let teammateName
let slackTeammateId
getTeammateInfo(teammateId).then(r => {
teammateName = r
slackID(teammateName.email).then(r2 => {
slackTeammateId = r2
)}
);

then() method on the other hand waits until your method's return and then fires out everything in the callback function.
Hopefully it helped you and sorry for my bad english :)

  • Related