You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Google Cloud Functions中如何等待依赖异步函数的返回值?

问题:异步函数依赖调用的执行顺序问题

场景背景

  • 数据流:Intercom Webhook → Google Cloud Functions(处理异步返回值)→ 向Slack发送消息

问题详情

原有代码运行正常,但当第二个异步函数需要使用第一个异步函数的返回值作为参数时,无法正确让第二个函数等待第一个执行完成后再触发。

相关代码

获取团队成员信息的异步函数

// 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);
  }
};

获取Slack ID的异步函数(依赖第一个函数的返回值)

// 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);
    }
  }
};

Google Cloud Functions主执行函数

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 })
  }

}

尝试过的方法

尝试使用Promise.all,但无法正确实现依赖调用:

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

编辑补充

代码本身逻辑没有问题,最终发现是slackID函数中填写了错误的API地址。


内容的提问来源于stack exchange,提问作者Jaroslav Melich

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.20 21:24:28