yargs/yargs

`yargs.parse(argv,callback)` issues call back before handler promise is finished.

開放

#1,069 建立於 2018年2月12日

 (24 則留言) (15 個反應) (0 位負責人)JavaScript (1,053 個分叉)batch import
Help Wantedfeature requesttriaged

倉庫指標

星標
 (10,829 顆星)
PR 合併指標
 (平均合併 113天 19小時) (30 天內合併 11 個 PR)

描述

yargs v11.0.0

When using yargs.parse() with a callback, expected behaviour is for callback to be called after yargs parse has completed. It is also expected that command handlers accept promises in their returns.

Example code:

const yargs = require('yargs');

yargs.command({
    command: 'test',
    description: 'a test command',
    handler: () => {
        return new Promise((resolve) => {
            setTimeout(() => {
                console.log('[Promise] finally executed');
                resolve('some result');
            }, 3000);
        });
    }
});

const callback = ()=>{
    console.log('[Yargs] Parse finished');
};

yargs.parse('test', (err, argv, output) => {
    callback(err, output);
});

console.log('[File] End');

outputs:

$ node index.js
[Yargs] Parse finished
[File] End
[Promise] finally executed 

expected:

[File] End
[Promise] finally executed 
[Yargs] Parse finished

Context: I'm using yargs inside REPL and need the callback to fire after the command has finished processing, so REPL can continue correctly.

Possibly linked issues: #510 : Adds in the Async handler support, but does not halt parse execution or pass promise back to user. #918 : possibly a feature request for this? #564 : Unanswered question on how to achieve this behaviour in yargs (though link provided to user space workaround/patch)

Workaround is discussed in #918 by attaching your promise to the argv and then checking it in the callback.

I would say that the callback in yargs.parse being called before command finishes could be seen as a bug, unless documentation is written to specify that it is expected behaviour and that async handlers will not halt execution of parse's callback.

貢獻者指南