Tuesday, 23 May 2017

CallbackHandler in Node.js

I have the following method in a .js file for an asynchronous network connection

function requestWatsonDiscovery (queryString){
console.log('Query =',queryString);
if(typeof queryString !== 'undefined'){
    var discoveryUrl = `something`
console.log('Total url =',discoveryUrl);
var options = {
    host: 'gateway.watsonplatform.net',
    path: discoveryUrl,
    auth : auth
        };

http.get(options, function (http_res) {
// initialize the container for our data
var data = "";

// this event fires many times, each time collecting another piece of the response
http_res.on("data", function (chunk) {
    // append this chunk to our growing `data` var
    //console.log(data);
    data += chunk;

});

    // this event fires *one* time, after all the `data` events/chunks have been gathered
    http_res.on("end", function () {
    // you can use res.send instead of console.log to output via express
    //console.log(data);
    data =JSON.parse(data);
    watsonReturnedText = parseData(data);
    //console.log(watsonReturnedText);
    //returnResult();

});});

}}  

at the same time I am updating my UI in another .js file.I want to get notified when the above asynchronous method has completed. I understand I can use callbacks /promises to do it.Can you show me the syntax to write a promise or a call back.



via GhostCode

No comments:

Post a Comment