Skip to content

Commit

Permalink
Fix bug #153 out of order results
Browse files Browse the repository at this point in the history
  • Loading branch information
mokkabonna committed Sep 25, 2023
1 parent d46752f commit a8395f2
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -237,11 +237,11 @@ class AutocompletePrompt extends Base {
}

// Store this promise for check in the callback
const lastPromise = thisPromise;
this.lastPromise = thisPromise;

return thisPromise.then((choices) => {
// If another search is triggered before the current search finishes, don't set results
if (thisPromise !== lastPromise) return;
if (thisPromise !== this.lastPromise) return;

this.currentChoices = new Choices(choices);

Expand Down
38 changes: 38 additions & 0 deletions test/spec/indexSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,44 @@ describe('inquirer-autocomplete-prompt', () => {
});
});

it('does not show previous result if later result finishes before it', () => {
const source = sinon.stub();
rl = new ReadlineStub();
prompt = new Prompt(
{
message: 'test',
name: 'name',
source,
},
rl
);

// even this finishes after the second one, it should not be shown since a new call has been made
const promise1 = new Promise((res) => {
setTimeout(() => {
res(['result1']);
}, 100);
});

const promise2 = new Promise((res) => {
setTimeout(() => {
res(['result2']);
}, 10);
});
source.onCall(0).returns(promise1);
source.onCall(1).returns(promise2);

const runPromise = prompt.run();
type('c');

return promise1.then(() => {
enter();
return runPromise.then((answer) => {
assert.deepEqual(answer, 'result2');
});
});
});

describe('suggestOnly = false', () => {
beforeEach(() => {
defaultChoices = ['foo', new inquirer.Separator(), 'bar', 'bum'];
Expand Down

0 comments on commit a8395f2

Please sign in to comment.