Saturday 3 June 2017

Can't print result of a function

I am trying to use the Watson Translator API to translate a user given input "input", and store it in a global variable "res" for later use in the Watson Conversation service, but I find myself unable to print or verify if the value was truly affected to my variable "res", here is my code :

var res;

function Translate(input, res){

 language_translator.translate({
    text: input,
    source: 'en',
    target: 'fr'
 }, function(err, data) {
    if (err)
        console.log(err)
    else {
        console.log('within translate function data is : '+ data.translations[0].translation);
        show(data.translations[0].translation);
    }
 });


 let show = function(data){
    console.log('within show function data is : '+ data);
    res = data;
    console.log('res inside show function : '+res);
 }

}

Translate('hello', function(){
  print();
});


function print(){
  console.log('final : '+res);
}

Result prints as follows :

within translate function data is : Bonjour
within show function data is : Bonjour
res inside show function : Bonjour



via Joe25

No comments:

Post a Comment