Skip to content

Commit a8395f2

Browse files
committed
Fix bug #153 out of order results
1 parent d46752f commit a8395f2

File tree

2 files changed

+40
-2
lines changed

2 files changed

+40
-2
lines changed

index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -237,11 +237,11 @@ class AutocompletePrompt extends Base {
237237
}
238238

239239
// Store this promise for check in the callback
240-
const lastPromise = thisPromise;
240+
this.lastPromise = thisPromise;
241241

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

246246
this.currentChoices = new Choices(choices);
247247

test/spec/indexSpec.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,44 @@ describe('inquirer-autocomplete-prompt', () => {
298298
});
299299
});
300300

301+
it('does not show previous result if later result finishes before it', () => {
302+
const source = sinon.stub();
303+
rl = new ReadlineStub();
304+
prompt = new Prompt(
305+
{
306+
message: 'test',
307+
name: 'name',
308+
source,
309+
},
310+
rl
311+
);
312+
313+
// even this finishes after the second one, it should not be shown since a new call has been made
314+
const promise1 = new Promise((res) => {
315+
setTimeout(() => {
316+
res(['result1']);
317+
}, 100);
318+
});
319+
320+
const promise2 = new Promise((res) => {
321+
setTimeout(() => {
322+
res(['result2']);
323+
}, 10);
324+
});
325+
source.onCall(0).returns(promise1);
326+
source.onCall(1).returns(promise2);
327+
328+
const runPromise = prompt.run();
329+
type('c');
330+
331+
return promise1.then(() => {
332+
enter();
333+
return runPromise.then((answer) => {
334+
assert.deepEqual(answer, 'result2');
335+
});
336+
});
337+
});
338+
301339
describe('suggestOnly = false', () => {
302340
beforeEach(() => {
303341
defaultChoices = ['foo', new inquirer.Separator(), 'bar', 'bum'];

0 commit comments

Comments
 (0)