简体   繁体   English

如何从另一个等待变量中等待值

[英]how to await value from another await variable

I cannot figure out how should I construct my code.我不知道应该如何构建我的代码。

Basic info:基本信息:

webhook (intercom) --> google cloud functions (await values) --> post message to slack. webhook(对讲)--> 谷歌云功能(等待值)--> 将消息发布到 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.我的代码工作正常,直到我需要从另一个 await function 获取值,我不确定如何“暂停”第二个 await 直到第一个 await 完成。

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 (它在 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我试过 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.谢谢你。

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. Promise.all()的问题在于它同时触发两个函数并等待两个函数完成,所以你不能这样链接。

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.另一方面, then()方法等到您的方法返回,然后触发回调 function 中的所有内容。
Hopefully it helped you and sorry for my bad english:)希望它对您有所帮助,并为我糟糕的英语感到抱歉:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM