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

调用异步方法返回Promise{<pending>},如何获取PaidUnpaidLeaveSchema数据?

解决async函数返回Promise而非实际数据的问题

你的问题出在调用async函数时没有使用await关键字。updatePaidLeave是async函数,这类函数默认返回Promise对象,只有用await等待它执行完成,才能拿到内部获取的实际数据。

修改后的代码如下:

static async updatePaidLeave() {
  const result = await PaidUnpaidLeaveSchema.find({});
  console.log(result, ";;;;;;;");
  return result; // 可选:如果需要在外部使用查询到的数据,返回结果
}

static runCronJob() {
  cron.schedule("*/10 * * * * *", async () => {
    // 用await等待async函数执行完成,拿到实际数据
    const leaveData = await this.updatePaidLeave();
    console.log(leaveData, "Im running in every 10 seconds.");
  });
}

关键修改说明

  • 在cron回调里调用this.updatePaidLeave()时添加await,确保异步操作完成后再获取结果
  • 可选在updatePaidLeave中返回result,这样外部可以直接拿到查询到的带薪休假数据做后续处理
  • 因为cron的回调已经声明为async,所以可以直接在内部使用await语法

内容的提问来源于stack exchange,提问作者Reyaj Alam

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.14 05:45:49