setTimeout的问题,试图等待异步执行 [英] setTimeout issue trying to wait for execution of async
问题描述
我是新来这种问题在javascript中,我无法修复这种企图等待异步调用结合角承诺的对象和超时。
功能onTimeout似乎从来执行。
getAsyncContent:功能(asyncContentInfos){ 变种deferObj = $ q.defer();
VAR promiseObj = deferObj.promise;
asyncContentInfos.promiseObject = promiseObj;
VAR blockingGuard = {做:假}; promiseObj.then(函数(){
blockingGuard.done = TRUE;
}); this.wait =功能(){
VAR执行= FALSE;
VAR onTimeout =功能(){
的console.log(************超时达到************);
执行= FALSE;
}; 而(!blockingGuard.done){
如果(执行和放大器;!&安培;!blockingGuard.done){
执行= TRUE;
的setTimeout(onTimeout,200);
}
}
}; $ http.get(asyncContentInfos.URL,{缓存:真})
。然后(功能(响应){
asyncContentInfos.responseData = response.data;
的console.log((getAsyncContent)asyncContentInfos.responseData(赛格瑞对象));
的console.log(asyncContentInfos.responseData);
deferObj.resolve('(getAsyncContent)解析);
blockingGuard.done = TRUE;
返回/*deferObj.promise*/ /*response.data*/;
},功能(errResponse){
VAR ERR_MSG ='(getAsyncContent)错误 - '+ errResponse;
deferObj.reject(ERR_MSG);
console.error(ERR_MSG);
}); 返回{
等待:this.wait
}
}
客户端code是这样的:
VAR asyncVocabulary =新AsyncContentInfos(BASE_URL +'taxonomy_vocabulary.json');
getAsyncContent(asyncVocabulary).wait();
和AsyncContentInfos是:
函数AsyncContentInfos(URL){
this.URL =网址;
this.responseData = [];
this.promiseObject;
}
$ http.get
返回调用完成时,这将解决一个承诺。承诺是一种方法,使asyncronous更清洁,直比普通的旧回调衬里。
getAsyncContent:功能(asyncContentInfos){ 返回$ http.get(asyncContentInfos.URL,{缓存:真})
。然后(功能(响应){
返回response.data;
},功能(errResponse){
console.error(ERR_MSG);
扔errResponse;
});
}
然后使用它:
getAsyncContent({...}),然后(功能(yourDataIsHere){});
有关承诺的好处是,他们可以很容易地链接起来。
getAsyncContent({...})
。然后(功能(yourDataIsHere){
返回anotherAsyncCall(yourDataIsHere);
})
。然后(功能(your2ndDataIsHere){
});
I'm new to this kind of problem in javascript and i can't fix this attempt to wait for an asynchronous call combining Angular promise objects and timeouts.
The function onTimeout seems never execute.
getAsyncContent: function (asyncContentInfos) {
var deferObj = $q.defer();
var promiseObj = deferObj.promise;
asyncContentInfos.promiseObject = promiseObj;
var blockingGuard = { done: false };
promiseObj.then(function () {
blockingGuard.done = true;
});
this.wait = function () {
var executing = false;
var onTimeout = function () {
console.log("******************** timeout reached ********************");
executing = false;
};
while (!blockingGuard.done) {
if (!executing && !blockingGuard.done) {
executing = true;
setTimeout(onTimeout, 200);
}
}
};
$http.get(asyncContentInfos.URL, { cache: true })
.then(function (response) {
asyncContentInfos.responseData = response.data;
console.log("(getAsyncContent) asyncContentInfos.responseData (segue object)");
console.log(asyncContentInfos.responseData);
deferObj.resolve('(getAsyncContent) resolve');
blockingGuard.done = true;
return /*deferObj.promise*/ /*response.data*/;
}, function (errResponse) {
var err_msg = '(getAsyncContent) ERROR - ' + errResponse;
deferObj.reject(err_msg);
console.error(err_msg);
});
return {
wait: this.wait
}
}
Client code is something like this:
var asyncVocabulary = new AsyncContentInfos(BASE_URL + 'taxonomy_vocabulary.json');
getAsyncContent(asyncVocabulary).wait();
And AsyncContentInfos is:
function AsyncContentInfos(URL) {
this.URL = URL;
this.responseData = [];
this.promiseObject;
}
$http.get
returns a promise which will resolve when the call completes. Promises are a way to make asyncronous more clean and straight lined than plain old callbacks.
getAsyncContent: function (asyncContentInfos) {
return $http.get(asyncContentInfos.URL, { cache: true })
.then(function (response) {
return response.data;
}, function (errResponse) {
console.error(err_msg);
throw errResponse;
});
}
Then using it:
getAsyncContent({...}).then(function(yourDataIsHere) {
});
The nice thing about promises is that they can be easily chained.
getAsyncContent({...})
.then(function(yourDataIsHere) {
return anotherAsyncCall(yourDataIsHere);
})
.then(function(your2ndDataIsHere) {
});
这篇关于setTimeout的问题,试图等待异步执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!