【问题标题】:JS init function not waiting for another function to completedJS初始化函数不等待另一个函数完成
【发布时间】:2021-08-06 23:11:42
【问题描述】:

在我的 JS 中,我想等待这个 fnDocumentTypeCount 被强制执行,然后再进入另一个 init 的逻辑,但它不会等待该函数完成。fnDocumentTypeCount 是我的 $.ajax会返回一个数字。

    init: function () {
    var countType = fnDocumentTypeCount(GroupId);
    console.log(countType);
    }

【问题讨论】:

标签: javascript


【解决方案1】:

使用await:

init: async function () {
  var countType = await fnDocumentTypeCount(GroupId);
  console.log(countType);
}

或者这是这样做的旧方法:

init: function () {
  fnDocumentTypeCount(GroupId).then(countType => {
    console.log(countType);
  });
}

【讨论】:

  • 是什么让你认为 fnDocumentTypeCount 返回了一个承诺?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-30
  • 2022-11-17
  • 1970-01-01
  • 2014-08-30
相关资源
最近更新 更多