File tree Expand file tree Collapse file tree 2 files changed +40
-2
lines changed Expand file tree Collapse file tree 2 files changed +40
-2
lines changed Original file line number Diff line number Diff line change @@ -237,11 +237,11 @@ class AutocompletePrompt extends Base {
237
237
}
238
238
239
239
// Store this promise for check in the callback
240
- const lastPromise = thisPromise ;
240
+ this . lastPromise = thisPromise ;
241
241
242
242
return thisPromise . then ( ( choices ) => {
243
243
// 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 ;
245
245
246
246
this . currentChoices = new Choices ( choices ) ;
247
247
Original file line number Diff line number Diff line change @@ -298,6 +298,44 @@ describe('inquirer-autocomplete-prompt', () => {
298
298
} ) ;
299
299
} ) ;
300
300
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
+
301
339
describe ( 'suggestOnly = false' , ( ) => {
302
340
beforeEach ( ( ) => {
303
341
defaultChoices = [ 'foo' , new inquirer . Separator ( ) , 'bar' , 'bum' ] ;
You can’t perform that action at this time.
0 commit comments