Methods
execute(context) → {Promise}
The execute method is used to run the command. Normally this is taken
care of by the controller and you don't have to worry about it at all. When you start
developing your own subcommands the following is very important:
1) Each subcommand that extends this abstract Command-class must override the execute()-method.
This is the entry and exit point, for your own implementation.
2) The execute() method must have a 'context'-parameter.
3) The execute() method must return a new Promise.
4) The promise must resolve/reject with a value of type CommandResult.
Parameters:
Name | Type | Description |
---|---|---|
context |
CommandContext | The context object is used to inject the data that is required to successfully execute the command. It contains all configurations and all commandResult-objects of any previous subcommands in the same macro. So you don need any other parameters. You can even add extra parameters to the context on-the-fly, and the new parameters will be available to any subcommand running after this one. (in the same macro) |
- Source:
Returns:
- The value of the promise should be this.result in most cases.
- Type
- Promise
Example
Sample subcommand extending this abstract Command class and overriding the 'execute()' method
Command = require('macro-command').Command;
class Example extends Command {
execute(context) {
var key = this.constructor.name;
this.context = context;
var self = this;
var cmdConf = context.parameters.cmdConfiguration;
return new Promise(function (resolve, reject) {
//CHECK REQUIRED INPUT PARAMETERS ...
//DO SOMETHING HERE ...
self.result.cmd = key;
self.result.data.push({ 'some example key': 'some example value' });
self.result.msg = 'The result from ' + key;
resolve(self.result);
});
}
}
module.exports = Example;