The project I am working on basically asks users for words they want defined, and then the program pulls in and returns those definitions using API.
What I am trying to accomplish is a flow like this:
- ask user for initial word
- take user input, feed it to API
retrieve and print API result
ask user for another word they want defined (or, if user types "exit app," the program will close)
- retrieve and print API result
- loop through 4 & 5 until user types "exit app"
I am running this in Node and have been unable to replicate the above flow. The API functionality is good, but users are getting asked the second question before the results for the first question have been returned. I want the second question to be asked only after the results for the first question have been retrieved and printed.
I have played around with ordering the code in various ways like putting the function asking the second question in the end of the code block for the function asking the first question, which ends up printing the second question before the results of the first one have been obtained.
My understanding of the issue is that it has to do with Node's asynchronous nature, but I am not quite sure how to achieve the flow I'm seeking. Would it be incorporating promises into the code or using something like the async module? Or is it not possible?
The current code block I have for the dictionary query:
var dict = new Oxford(config);
var dictQuery = function(word) {
var lookup = dict.definitions(word);
return lookup.then(function(res) {
var results = res.results;
var lexicalEntries = results[0]['lexicalEntries'];
for(var i=0; i < lexicalEntries.length; i++) {
var objectContainingEntries = lexicalEntries[i];
var entries = objectContainingEntries['entries'];
var objectContainingSenses = entries[0];
var senses = objectContainingSenses['senses'];
for(var j=0; j < senses.length; j++) {
var objectContainingDefs = senses[j];
var defsList = objectContainingDefs['definitions'];
for(var k=0; k < defsList.length; k++) {
var definition = defsList[k].trim();
}
console.log("- " + definition);
}
}
})
};
The dictionary module I'm using seems to suggest var lookup returns a promise.
The current code block I have for the questions:
var askUser = function() {
rl.setPrompt("Please enter the word you would like to define: ");
rl.prompt();
rl.on('line', function(word) {
dictQuery(word.toLowerCase().trim());
askAgain();
})
}
var askAgain = function() {
rl.setPrompt("Is there another word you would like to define? (enter word, or type 'exit app' to leave) ");
rl.prompt();
rl.on('line', function(nextWord) {
if (nextWord.toLowerCase().trim() === 'exit app') {
rl.close()
} else {
dictQuery(nextWord);
askAgain();
}
})
}
askUser();
Thank you for any insight and help you can provide!
*Note: I believe my question differs from the one marked as a possible duplicate because in my case, the second function does not need data generated by the first function to be fed into it.