如何在JavaScript中用Promise替换'Async = false'? [英] How to replace 'Async=false' with promise in javascript?
本文介绍了如何在JavaScript中用Promise替换'Async = false'?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经阅读了很多有关 promises
的信息,但是我仍然不确定如何实现它.
I have read a lot about promises
but I'm still not sure how to implement it.
我使用 async = false
编写了以下AJAX调用,以使其正常工作,但是我想用promise代替它,因为我看到 async = false
是不推荐使用.
I wrote the folowing AJAX call with async=false
in order for it to work, but I want to replace it with promise as I saw that async=false
is deprecated.
self.getBalance = function (order) {
var balance;
$.ajax({
url: "/API/balance/" + order,
type: "GET",
async: false,
success: function (data) {
balance = data;
},
done: function (date) {
}
});
return balance;
}
您能帮我吗?我只需要一个例子来理解它.
Would you be able to help me? I just need an example to understand it.
推荐答案
从 getBalance
方法返回promise对象:
Return promise object from getBalance
method:
self.getBalance = function (orderNumber) {
return $.ajax({
url: "/Exchange.API/accountInfo/balance/" + orderNumber,
type: "GET"
});
}
并在以后像这样使用它:
and use it later like this:
service.getBalance().then(function(balance) {
// use balance here
});
这篇关于如何在JavaScript中用Promise替换'Async = false'?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文