本文介绍了Asynchronus在angularjs调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是新来的Javascript和Angularjs。我想知道,如何调用函数异步,而不必等待它从它返回。
I am new to Javascript and Angularjs. I wanted to know , how to call a function asynchronously without waiting for it to return from it.
请让我知道,如果有一些例子那么这将是非常有益的。
Please let me know and if there is some example then it would be very helpful.
问候,
了nG
Regards,nG
推荐答案
使用角的:
function myAsyncFunction() {
var deferred = $q.defer();
//..
setTimeout(function() {
deferred.resolve({ message: "resolved!" });
// or deferred.reject({ message: "something went terribly wrong!!" });
}, 1000);
//..
return deferred.promise;
}
myAsyncFunction()
.then(function(data){
// success
console.log("success", data.message);
}, function(data) {
// fail
console.log("error", data.message);
}).finally(function() {
// always
});
这篇关于Asynchronus在angularjs调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!