Wednesday, 7 June 2017

Javascript pass value to variable from a callback function

I would like to pass value to a variable from a callback function.

Is it possible for asynchronous call? like add settimeout,etc

Below is my code and x returns undefined.

var surl ="https://www.google.com";

function doCall(surl,callback){ 
    request(surl, function (err, response,html) {
        callback(html); 

    });
};

var x = doCall(surl, function(returnval){
 console.log(returnval);
});

console.log("x="+x); <---undefined



via codesheep

No comments:

Post a Comment