簡體   English   中英

不要執行 function 直到另一個函數的 ajax 調用完成

[英]Do not execute function until another function's ajax call finishes

我已經嘗試搜索了一段時間,但沒有找到解決這種情況的方法。 Say I have a function that executes an ajax call, but the result of the ajax is not returned, and I cannot modify this function, I can only call it. I need to call this function and then execute another function once the ajax call in the former function is complete.

這是我嘗試的眾多解決方案之一的示例:

function doAjaxCall() {
  $.ajax({
    url: "https://api.publicapis.org/entries"
  }).done(function(response) {
    console.log("I want this message to log first");
  });
}

function anotherCall() {
  console.log("and this message to log second");
}

promise = new Promise(() => {
  doAjaxCall();
});

$.when(
  promise,
).then(
  (r1) => { anotherCall(); }
);  

這不起作用; 在 ajax 調用完成之前, anotherCall()仍在執行。 同樣,在此示例中,我無法修改doAjaxCall()的內容。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM