diff --git a/lib/api/_loaders/command.js b/lib/api/_loaders/command.js index 3afd576d5..da1ac0579 100644 --- a/lib/api/_loaders/command.js +++ b/lib/api/_loaders/command.js @@ -183,7 +183,16 @@ class CommandLoader extends BaseCommandLoader { }) .catch(err => { if (instance instanceof EventEmitter) { - instance.emit('error', err); + if (instance.needsPromise) { + // if the instance has `needsPromise` set to `true`, the `error` event is listened + // on the `context` object, not on the `instance` object (in `treenode.js`). + this.emit('error', err); + } else { + // for class-based commands that inherit from EventEmitter. + // Since the `needsPromise` is set to `false` in this case, the `complete` and `error` + // events are listened on the `instance` object. + instance.emit('error', err); + } return; } diff --git a/lib/core/treenode.js b/lib/core/treenode.js index 1bfa4ae36..057cbb0fb 100644 --- a/lib/core/treenode.js +++ b/lib/core/treenode.js @@ -206,6 +206,9 @@ class TreeNode { commandResult = this.instance; if (this.instance.needsPromise) { this.needsPromise = true; + // this change was done because the only way function-styled custom commands could be resolved + // is if they contain another NW API command, which could call `node.context.emit('complete')` + // inside `asynctree.js > resolveNode` method for the custom command node. commandResult = this.context; } } else if (this.context instanceof EventEmitter) { // Chai assertions