Skip to content

Commit

Permalink
Fix unhandledRejection on error inside function-based custom comman…
Browse files Browse the repository at this point in the history
…ds. (#4309)
  • Loading branch information
garg3133 authored Nov 25, 2024
1 parent 35d2e7d commit 1d6e122
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
11 changes: 10 additions & 1 deletion lib/api/_loaders/command.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
3 changes: 3 additions & 0 deletions lib/core/treenode.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 1d6e122

Please sign in to comment.