简体   繁体   English

代码以错误的顺序执行并且它不是异步的(我认为......)

[英]Code executing in wrong order and it isn't async (I think…)

I have this code below.我在下面有这段代码。 Why does console.log("done:) execute before the loop finishes? I get it that "request" is async but my console.log is inside the callback function. The callback function is synchronous (as far as I can see) and starts going through the loop, then when the loop finishes it should execute my console.log("done"). But it doesn't. It executes it before (or right after the first iteration of the loop). Is the loop async too? It's the only way I could explain what is happening.为什么 console.log("done:) 在循环完成之前执行?我知道“请求”是异步的,但我的 console.log 在回调 function 内。回调 function 是同步的(据我所知)和开始遍历循环,然后当循环完成时,它应该执行我的 console.log("done")。但它没有。它在循环的第一次迭代之前(或在循环的第一次迭代之后)执行它。循环是否异步也是?这是我能解释发生了什么的唯一方法。

const request = require('request');

var item_urls;
var options = {
    json: true
  };
var test = [] ;


function updateDB (){
    var url = "https://api.warframe.market/v1/items";


    request(url, options, (error, res, body) =>{
        if (error) {
            return console.log(error)
          };

          if (!error && res.statusCode == 200) {
            console.log("executing cb1");
            item_urls = body.payload.items;
            var primes = item_urls.filter(item => item.item_name.includes("Strun Wraith Set"));
            for (item in primes) // Loop through items, then and add to recipelist. This is to make it the same name as the URL attribute.
            {
                let url = `https://api.warframe.market/v1/items/${primes[item].url_name}`;
               // console.log (url);
                request(url, options, (error, res, body) =>{
                    if (error) {
                        return console.log(error)
                      };

                      if (!error && res.statusCode == 200) {

                          console.log(`Getting item ${url}`);
                          test.push(body.payload.item);
                      }
                    });

            };  
            console.log ("done");          


          };
    });
}
updateDB ();

The for loop is NOT asynchronous and the console.log("done") does indeed run after the loop. for循环不是异步的,并且console.log("done")确实在循环之后运行。 However you are making an asynchronous request within the for loop which is why you're seeing the inner request callback's console.log afterwards.但是,您正在for循环中发出异步请求,这就是为什么您之后会看到内部请求回调的console.log的原因。

request(url, options, (error, res, body) => { ... })

Even without knowing anything about request , you can tell this is an asynchronous function invocation: instead of returning a result, it accepts a function to run on said result.即使对request一无所知,您也可以知道这是一个异步 function 调用:它不返回结果,而是接受 function 以在所述结果上运行。

Synchronous invocation would have looked like this:同步调用看起来像这样:

let [error, res, body] = request(url, options);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM