How to call function inside another action and get its return value in component's "actions" hook?


#1

How to call function inside another action and get its return value in component’s “actions” hook? For example, // components/some-comeponent.js

actions: {
     action1() {
          const value = this.send("action2", 40);
          console.log(value);
     },

     action2(someNumber) {
          return someNumber / 2;
     }
}

the output of executing of “action1” function is going to be undefined. I search for a solution, which outputs 20. Thank you


#2

Hi @Enterpub, is there any reason you want the second function to be an action? Typically actions are used as a way for the component to handle DOM-related logic, so what I usually do in a situation like this is instead of having an action send another action, just have the first action use a normal function attached to the component. For example:

action2(someNumber) {
  return someNumber / 2;
}
actions: {
  action1() {
    const value = this.action2(40);
    console.log(value);
 }
}

#3

@dknutsen thank you for reply. That is what I was looking for. The reason was to have a function, which has some common code. I was going to call the function in couple places in the component. Did not wanted to copy-paste.