Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

spellSuggest, promises, and express #55

Closed
samartioli opened this issue Mar 24, 2017 · 7 comments
Closed

spellSuggest, promises, and express #55

samartioli opened this issue Mar 24, 2017 · 7 comments

Comments

@samartioli
Copy link

spellSuggest does not behave as expected when the callback is wrapped in a promise.

I created a repo to demonstrate the issue

@trofim24
Copy link
Contributor

Very similar to nodejs/node#5691

@Wulf
Copy link
Owner

Wulf commented Sep 29, 2017

Hi @samartioli, thanks for reporting this and the great example you've setup!

I was able to reproduce this on linux! I tested this with the new nan branch, and the bug was no longer present (the promise resolved just fine). Thus, this will be fixed in the next major version ([email protected]).

In the next version, spellSuggest will no longer be present, only spellSuggestions is available. And this is renamed to suggest as per #37.

Below, I briefly outline some of the changes I made in order to test the nan branch here:

  1. Switch to the suggest function, instead of spellSuggest. origWord isn't passed in currently, so I use the utterance parameter instead later on instead.
  2. an array is always sent, so I had to check for suggestion.length > 0, instead of just if(suggestion).
  3. resolve to the first element in the array instead of the array.
function spellSuggestPromise(utterance) {
    return new Promise(function(resolve, reject) {
        dict.suggest(utterance, function(err, correct, suggestion, origWord) { // 1
            console.log(err, correct, suggestion, origWord);
            if (err) {
                // return setImmediate(reject, error);
                return reject(error);
            }
            if (suggestion.length > 0) { // 2
                return resolve(suggestion[0]); // 3
                // return setImmediate(resolve, suggestion);
            }
            return resolve(utterance);
            // return setImmediate(resolve, origWord);
        });
    });
}

Again, thanks for your patience and time setting up nodehun-promise-example. :)

@Wulf Wulf closed this as completed Nov 19, 2017
@ashutoshrishi
Copy link

I am currently experiencing this with the master branch, and resorting to using setImmediate for now. Should I be using the nan now ?

@Wulf
Copy link
Owner

Wulf commented May 29, 2018

The NAN implementation isn't complete (see #37). I'll look into this in the master branch.

@Wulf Wulf reopened this May 29, 2018
@loretoparisi
Copy link

@Wulf any update on this?
In the meanwhile my my wrapper I had to use the setImmediate as well like this

Hunspell.prototype.isCorrectP = function(word) {
        var self = this;
        return new Promise(function(resolve, reject) {
            self.dictionary.isCorrect(word, function(err, correct, origWord){
                // because "color" is a defined word in the US English dictionary
	            // the output will be: null, true, "color"
                if(err) reject(err)
                else return setImmediate(resolve, correct);
            });
        });
    };
Hunspell.prototype.spellSuggestionsP = function(word) {
        var self = this;
        return new Promise(function(resolve, reject) {
            self.dictionary.spellSuggestions(word, function(err, correct, suggestions, origWord){
                // because "calor" is not a defined word in the US English dictionary
	            // the output will be: null, false, [ 'carol','valor','color','cal or','cal-or','caloric','calorie'], 'calor'
                if(err) reject(err)
                else return setImmediate(resolve, suggestions);
            });
        });
    };
Hunspell.prototype.stemP = function(word) {
        var self = this;
        return new Promise(function(resolve, reject) {
            self.dictionary.stem(word, function(err, stems){
                // the output will be: null, [telling, tell]
                if(err) reject(err)
                else return setImmediate(resolve, stems);
            });
        });
    };

@Wulf Wulf mentioned this issue Nov 10, 2019
@Wulf
Copy link
Owner

Wulf commented Nov 13, 2019

@samartioli @ashutoshrishi @loretoparisi thanks for your patience everyone.

Async methods return Promises directly now in the n-api branch.
They will be featured in 3.0.0 which should be released in the following few days.

If you're still around, please let me know if the new version fixes the issue:

npm i -S wulf/nodehun#n-api

The API has changed a bit, but feel free to reference the type declaration file.

@Wulf
Copy link
Owner

Wulf commented Nov 24, 2019

I got a chance to verify that this doesn't occur with Nodehun v3 on node version 10, 11 and 12.

I changed the definition of the spellSuggestPromise function in samartioli's repository to the following:

function spellSuggestPromise(utterance) {
    return dict.suggest(utterance)
}

Thanks again for reporting this in a detailed manner and making it easy for us to reproduce the error. I'm going to close this issue, but feel free to reopen it or continue the discussion.

@Wulf Wulf closed this as completed Nov 24, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants