简体   繁体   English

HTTP请求循环完成后,将执行下一个功能

[英]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.

相关问题 可变大小循环内的http请求,只有在最后一个请求完成后才能做某事(JavaScript) - http requests inside loop of variable size, how to do something only after last request has finished (JavaScript) 请求循环完成后如何获得响应 - How to get response after Loop of requests finished 函数完成后继续执行FOR循环 - Continue FOR loop after a function finished 函数完成后,Node.js回调下一个循环 - Nodejs callback next loop when function is finished 当前效果100%完成后不执行回调function - A callback function is not executed after the current effect is 100% finished 循环结束后如何发送函数的结果? - How to send result of a function after the loop is finished? 仅在来自 axios 的请求的循环完成后执行代码块 - Execute block of code only after loop with requests from axios is finished 执行 map() 循环后如何调用 function - How to call a function after map() loop executed for循环执行完后,在for循环中执行的Mongoose查询不会被推入空数组中 - Mongoose Query executed in for loop is not being pushed into an empty array after the for loop in finished executing, nodejs 循环完成后,将调用循环内的函数 - function within loop gets called after loop has finished
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM