0
var request = require('request');

var options = {
  url: 'https://api.github.com/repos/request/request',
  headers: {
    'User-Agent': 'request'
  }
};

function callback(error, response, body) {
  if (!error && response.statusCode == 200) {
    var info = JSON.parse(body);
    console.log(info.stargazers_count + " Stars");
    console.log(info.forks_count + " Forks");
  }
}
exports.parse = function(){
   request(options, callback);
}

Hi, how can i get info variable located inside callback function. Sorry, i'm beginner in js and didn't find answer inside my brain / peace

3
  • If you need to use that variable else where, why not have request accept a custom callback? Commented Mar 29, 2017 at 18:01
  • Can't find the duplicate right now. Someone please close this with the correct duplicate Commented Mar 29, 2017 at 18:02
  • @user2896976 this implementation localizes the code :) Commented Mar 29, 2017 at 18:05

3 Answers 3

2

The callback functions are used to handle the non-blocking - asynchronous nature. When a job is blocking the I/O it simply put it in the task queue and moves forward. After finishing the task it is time to trigger the callback function, we can handle those returned values in any way we want. That's the use of callback functions. You can read about event-loop for better understanding.

Here you have to pass the custom callback function to properly handle the scenario.

var request = require('request');

var options = {
  url: 'https://api.github.com/repos/request/request',
  headers: {
    'User-Agent': 'request'
  }
};

exports.parse = function(cb){
   request(options, cb);
}

In the place where you want to use the parse method use like this..

parse(function(error, response, body) {
  if (!error && response.statusCode == 200) {
    var info = JSON.parse(body);
    console.log(info.stargazers_count + " Stars");
    console.log(info.forks_count + " Forks");

// TODO with info

  }
});
Sign up to request clarification or add additional context in comments.

Comments

0

declaring a variable on a higher scope will make it accessible. The value in the higher scope will not be updated until the callback is called.

var result = {};
function callback(error, response, body) {
    if (!error && response.statusCode == 200) {
        result.info = JSON.parse(body);
        console.log(result.info.stargazers_count + " Stars");
        console.log(result.info.forks_count + " Forks");
    } 
}

2 Comments

this method return me {} empty result object , when i call result in parse function :|
result object is empty until callback has occured. All your logic should be contained within the callback function. You can also use promises if you want better control with how to handle result when a result is available.
0

If your goal is simply to have your body parsed and have the parsed JSON available readily, you should consider using a middleware such as body-parser. It is a standard pattern... Consider this code for example: (from https://github.com/expressjs/body-parser)

var express = require('express')
var bodyParser = require('body-parser')

var app = express()

// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }))

// parse application/json
app.use(bodyParser.json())

app.use(function (req, res) {
  res.setHeader('Content-Type', 'text/plain')
  res.write('you posted:\n')
  res.end(JSON.stringify(req.body, null, 2))
})

1 Comment

thx, but no. info variable can be equal var info = parseInformation(body);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.