Node.js - Asynchronous JSON Query

前端 未结 1 1548
囚心锁ツ
囚心锁ツ 2021-01-26 12:53

I apologize if this is a stupid question, but I am new to Javascript and Node.js really hurts my head because it is asynchronous.

My goal is to query for a JSON object f

相关标签:
1条回答
  • 2021-01-26 13:38

    The function you are creating with the line

    request(url, function (error, response, body) {
    

    is not executed until the response is received. The rest of your code continues to run. Think of the flow something like this:

    var request = require('request');
    
    var url = 'url here';
    var temp;
    var done = false;
    
    request(url, XXX);
    
    if (done){
      console.log(temp);
    

    then when the response is received (perhaps much later on) the function XXX is executed.

    As you can see, done will always be false when the line

    if (done){
    

    is executed.

    0 讨论(0)
提交回复
热议问题