[英]After a loop of HTTP requests finished, next function will be executed
I need to call the next function after a loop of HTTP requests is done and the number of requests depend on the number of input.
HTTP请求循环完成后,我需要调用下一个函数 , 请求的数量取决于输入的数量。
I tried to use Promise to make sure that all HTTP requests are done . 我试图使用Promise来确保所有HTTP请求都已完成 。 But it doesn't work.. I don't know how to solve this...
但这不起作用..我不知道该怎么解决...
var countA,countB,countC,countD,countE,countF = 0
checkFirst(){
Promise.all([this.checkWords()]).then( values => {
this.doSomething()
}
}
doSomething(){
// add value into database
}
and I need to run this function in the checkFirst() function and check it with Promise.all function.. 我需要在checkFirst()函数中运行此函数,并使用Promise.all函数进行检查。
Then, this is my HTTP request code... 然后,这是我的HTTP请求代码...
checkWords(){
let promise = new Promise(function (onFulfilled, onRejected) {
let input = "Words for send request";
let words = ['A', 'B', 'C', 'D', 'E', 'F'];
let wordsCount = [0, 0, 0, 0, 0, 0];
let inputSplitted = input.split(" ", ms.length);
for (let i = 0; i < inputSplitted.length; i++) {
this.http.get("url + inputSplitted , options)
.subscribe(res => {
// content is a string that contain a word of A/B/C/D/E or F
let content = res['_body'].entryContent
let wordlevelIndex = 0 // For indexing "words" array
let lvlfound = false
while (!lvlfound) {
// Index of the string A,B,C,D,E,F in the respond
let strIndex = content.search(words[wordlevelIndex])
if (strIndex == -1) { // -1 = not found A,B,C,D,E,F
wordlevelIndex++
} else {
// Substring that respond for getting string A,B,C,D,E,F
let lvlIndex = content.substring(strIndex, strIndex + 1);
console.log(lvlIndex)
// Count the word that found
if (lvlIndex == words[0]){
wordsCount[0]++
}else if (lvlIndex == words[1]){
wordsCount[1]++
}else if (lvlIndex == words[2]){
wordsCount[2]++
}else if (lvlIndex == words[3]){
wordsCount[3]++
}else if (lvlIndex == words[4]){
wordsCount[4]++
}else if (lvlIndex == words[5]){
wordsCount[5]++
}
lvlfound = true
wordlevelIndex++
}
}
});
}
onFulfilled([wordsCount[0], wordsCount[1], wordsCount[2], wordsCount[3], wordsCount[4], wordsCount[5]]);
onRejected('Error');
})
promise.then(([arg0, arg1, arg2, arg3, arg4, arg5]) => {
this.countA += arg0
this.countB += arg1
this.countC += arg2
this.countD += arg3
this.countE += arg4
this.countF += arg5
});
return promise
}
With these codes, I still got an asynchronous of the function which it call "this.doSomething" before all the requests is done . 有了这些代码, 在所有请求完成之前 ,我仍然得到了一个异步函数,它称为“ this.doSomething” 。 I only want all the responds done before using next function.
我只希望在使用next函数之前完成所有响应。 Is I do something wrong via using Promise?
使用Promise是否做错了什么? Help me please.
请帮帮我。 Thanks.
谢谢。
You're resolving (and rejecting!) the promise immediately after firing the request, way before the call returns. 触发请求后,即在调用返回之前,您正在解决(并拒绝!)诺言。 I think the resolution (your
onFulfilled
call) should be placed at the end of the subscribe
argument function. 我认为解决方案(您的
onFulfilled
调用)应放在subscribe
参数函数的末尾。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.