我有一个 JavaScript 函数,需要返回 true 才能更新浏览器 UI。如何确保函数在执行代码以进行后端调用之前返回?
self.resortCopy = function(item) {
self.resorts.push(item);
self.backendCall(item) // this needs to be performed after returning true
return true;
最佳答案
我想你在 self.backendCall (XHR/fetch) 中有一个 ajax 请求
如果这是真的,那么你的代码已经异步了
否则,看看@hemp的回答
function ajax(){
fetch("https://httpbin.org/get").then(data=>{
console.log("DONE")
})
}
function test(){
ajax();
return true;
}
console.log(test())
关于javascript - 从 javascript 函数返回一个值,然后执行其中的代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43791710/