-1

In my JS I want to wait for this fnDocumentTypeCount to be compelted before I go into the other with the logic for the init but it will not wait for that function to be completed.fnDocumentTypeCount is a $.ajax that I was going to return a number.

    init: function () {
    var countType = fnDocumentTypeCount(GroupId);
    console.log(countType);
    }
  • use a promise management! – Mister Jojo May 17 '21 at 16:52
  • 1
    Is it async? There's not enough context here to know what specifically you're asking. Please see the [How to Ask](https://stackoverflow.com/help/how-to-ask) page, and possibly [How do I return the response from an aynchronous call](https://stackoverflow.com/q/14220321/438992). – Dave Newton May 17 '21 at 16:54
  • Maybe this helps https://stackoverflow.com/questions/27759593/how-do-i-wait-for-a-promise-to-finish-before-returning-the-variable-of-a-functio#27759617 – Maicon Mauricio May 17 '21 at 16:55
  • although the code you showed looks fine, the answer lies in the code you didn't show. You may have been using an async or promise based function that you must await. look into promises. – Kevin Crum May 17 '21 at 16:55
  • I included that fnDocumentTypeCount is returning – Jefferson May 17 '21 at 17:23

1 Answers1

-1

Use await:

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

Or here's the old way of doing this:

init: function () {
  fnDocumentTypeCount(GroupId).then(countType => {
    console.log(countType);
  });
}
V Maharajh
  • 9,013
  • 5
  • 30
  • 31