0

In the code below, I am trying to get a value from a 'nodehun' method called spellSuggestions. In the documentation I'm told the syntax to use this method is as follows: dict.spellSuggestions(w, handleMisspellings); where w is a list of words and handleMisspellings is a function (which is posted below). I can see the output on the console for handleMisspellings, but for the life of me, I cannot return or find a way to return a variable from this call: [dict.spellSuggestions(w, handleMisspellings);]. After setting a var equal to 'dict.spellSuggestions(w, handleMisspellings);' the return value is undefined. Please help!

var debugFlag = process.argv.indexOf('debug') > -1;
var nodehun = require('./../build/' + (debugFlag ? 'Debug' : 'Release') + '/nodehun');
var fs = require('fs');

var dict = new nodehun(fs.readFileSync(__dirname+'/dictionaries/en_US.aff'),fs.readFileSync(__dirname+'/dictionaries/en_US.dic'));
//var words = ['original', 'roach', 'erasee', 'come', 'consol', 'argumnt', 'gage',
//           'libary', 'lisence', 'principal', 'realy', 'license', 'suprise', 'writting'];

var handleMisspellings = function(err, correct, suggestions, origWord, callback) {
    if (err) throw err;
    if (correct) {
        console.log(origWord + ' is spelled correctly!');

    }
    else {
        console.log(origWord + ' not recognized. Suggestions: ' + suggestions);
    }
    var value = {
        err: err,
        correct: correct,
        suggestions: suggestions,
        origWord: origWord
    };
    console.log('VALUE+++++: ' + value);
    return value;
}

var foo = function(words) {
        words.forEach(function(w) {
        dict.spellSuggestions(w, handleMisspellings);
        some = dict;
        console.log(JSON.stringify(some, null, 2));
        });
}
module.exports = {
        foo: foo
}
Evan Woods
  • 31
  • 1
  • 2
  • You don't; you provide a callback that that does what you want, same as https://stackoverflow.com/q/14220321/438992. Async programming, and callbacks/promises/await/etc are something you want to spend some time wrapping your head around fairly early on: you need it. I'm not even sure what you were doing with `some = dict` and trying to do stuff with it; re-assigning a reference doesn't change what it is. – Dave Newton Aug 06 '18 at 17:33
  • Thanks for the direction! I need the data inside of handleMiisspellings. That data is only generated by calling the method "spellSuggestions". I can't edit the method to accept a callback function. The values of the data [err, correct, suggestions, origWord] are inaccessible outside of the call of the method. – Evan Woods Aug 06 '18 at 22:51
  • I have another function called checkForLookupRequests where I implement a callback. In the code where I use this function, I use it by calling: checkForLookupRequests(data, function(err, data) { if(err) { return res.status;} else { return res.json(data);}}); In the definition of the function I declare checkForLookupRequests(data, callback) and eventually return a new value for data by calling callback(null, data) within the function definition. – Evan Woods Aug 06 '18 at 22:51

1 Answers1

0

Thanks Dave. I eventually discovered the practical use of callback functions. For each method that contained data that I desired to access outside of the method, I declared an individual function to wrap the method. The function accepted two input arguments. The first was the input variable to drive the method call. The second was literally 'callback'. Inside the method, I would perform whatever operation I wanted to package the data into a JSON object before returning any desired data with 'return callback(var)'. In the call of the created wrapper function, I would actually call the function using the input variable of choice to drive the method in the function definition, and pass 'function(return_variable)' as the second argument. This creates a new method in which the desired data may be accessed or even again called back. The final code I desired performs a for loop on each element of a list of words, creates metadata about those words, and appends the unique data for each word to each word in a single array. The final array is a single object which contains all input words, and all data about those words. It required 4 individual functions (one of which was recursive), and a function call. Please see the code snippet of the function described above [doCall]. Note the use of the code begins at the call of 'analyze' [which is commented out here] and works its way up to each previous function declaration. I hope this helps someone else in the future to understand the functional use of 'callbacks'. Please ask if you have any questions, and Thanks again =D.

function doCall(word, callback) {
    dict.spellSuggestions(word, function(err, correct, suggestions, origWord) {
        if (err) throw err;
//      if (correct)
//      console.log(origWord + ' is spelled correctly!');
//      else
//      console.log(origWord + ' not recognized. Suggestions: ' + suggestions);
        val = {
            err: err,
            correct: correct,
            origWord: origWord,
            suggestions: suggestions
        }
        return callback(val);
    });
}

function addMember(array, index, callback){
    doCall(array[index], function(val){
//      console.log(val);
//      console.log(index);
//      console.log(array[index]);
//      console.log(val.origWord);
        array[val.origWord] = val;
//      console.log(array[val.origWord]);
        index = index + 1;
        return callback(array, index);
    });
}

function Loop(array, index, callback) {
    addMember(array, index, function(array2, index2){
//      console.log(index);
//      console.log(index2);
        if(index2 === array2.length) {
            return callback(array2);
        }
        else{
            Loop(array2, index2, callback);
        }
    });
}

function analyze(array, index, callback){
    Loop(array, index, function(complete_array){
        console.log('!!!!!!!!!!!!!!!!!' + complete_array);
        return callback(complete_array);
    });
}
/*
analyze(words, 0, function(complete_array){
//  for(i = 0; i < complete_array.length; i++) {
            console.log(complete_array);        
//  }
});
*/
module.exports = {
    analyze
}   
Evan Woods
  • 31
  • 1
  • 2