Command Executor
Joshua Jung edited this page Feb 24, 2017
·
3 revisions
The Ringa Command
(src) class is more or less a glorified function
executor.
Example:
import {Command} from 'ringa';
export default class ShowTheNews extends Command {
execute(news /* injections */) {
console.log(`Here's todays happenings: ${someInjection}`);
}
}
One of the biggest advantages of Command
executors over a default function
executor is that they can be extended.
import MyCommand from './MyCommand';
export default class ShowTheNewsWithAttitude extends ShowTheNews {
execute(news, noAttitude) {
if (noAttitude) {
return super.execute(news);
}
console.log(`Yo, some things happened and stuff. Here ya go: ${news}`);
}
}
Like the function
executor, Command
executors can return a promise and Ringa will wait for the promise to resolve or be rejected before continuing (or killing the thread).
import {Command} from 'ringa';
export default class ShowTheNews extends Command {
execute(someAPI) {
return new Promise((resolve, reject) => {
someAPI.getTheNews().then(result => {
console.log(`Well, I guess there was some news today! ${result}`);
resolve(result);
}).catch(reject);
});
}
}
If you find yourself tempted to string Promises together in Ringa, they probably should be split into their own executors to make your code more readable and extensible.