Actions can call another Action #1475
-
We should enable the ability to chain actions together, for one action to call another. This would create the ability for request-level service objects to be used. Example usage: import { action, Action } from "actionhero";
export class RandomNumber extends Action {
constructor() {
super();
this.name = "randomNumber";
this.description = "I am an API method which will generate a random number";
this.outputExample = { randomNumber: 0.123 };
}
async run({ connection, response }) {
response.randomNumber = Math.random();
}
}
export class CombinedActon extends Action {
constructor() {
super();
this.name = "combinedAction";
this.description = "I let people know if the random number is big or small";
this.outputExample = { randomNumber: 0.123, big: true };
}
async run(data) {
// take the whole data argument and pass it to the other action
// will `throw` if something goes wrong
action.call('randomNunber', data)
// response and other properties will be modified by the first action
data.response.big = data.response.randomNumber > 0.5
}
} |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 1 reply
-
Hmm. As a fan of pure functions I don't know that I like this. If there is behavior that you want to share between actions why not move it into an initializer? |
Beta Was this translation helpful? Give feedback.
-
Some other thoughts on what this might allow:
|
Beta Was this translation helpful? Give feedback.
-
I'm just new in ActionHero, but it's one of my first question lol, Could we call Action To action? Could be nice |
Beta Was this translation helpful? Give feedback.
-
Here's a proposal to accomplish this! #1628 |
Beta Was this translation helpful? Give feedback.
Here's a proposal to accomplish this! #1628