-3

Is the naming convention an issue? doThis defines unique logic for my_function and calls the f_get or f_put accordingly as required.

Assume variable names will be defined properly. My question is regarding the re-defining of my_function every time in doThis functions.

Is there a better way to refactor this?

f_get(..., my_function) {
    axios...(...)
    .then(my_function)
}

f_put(..., my_function) {
    axios...(...)
    .then(my_function)
}

doThis1() {
    my_function = {...}
    f_get(..., my_function)
}

doThis2() {
    my_function = {...}
    f_put(..., my_function)
}

doThis3() {
    my_function = {...}
    f_get(..., my_function)
}
ashen
  • 1
  • 1
  • some usage scenarios? – user1514042 Sep 30 '19 at 15:35
  • Just a generic rest call js script. I am asking regarding the coding style/ convention – ashen Sep 30 '19 at 15:40
  • JavaScript is a functional language more than procedural one (the further it goes to a bigger extent), the way you deal with it exercises its dark procedural side, which isn't necessarily the best approach. – user1514042 Sep 30 '19 at 15:42

1 Answers1

0

Here's a bit more idiomatic way of doing what you do (postprocessor = my_function):

 process({ verb, parameters, postprocessor }) {

        const { handler, postprocessor } = ({
            put: {
                hander: parameters => axios.put(parameters)
            },
            get: {
                hander: parameters => axios.get(parameters)
            }
        })[verb];

        return handler(parameters).then(postprocessor)
    }

And then you can curry it like this:

const processPostprocessorA = ({ verb, handler }) => process({ verb, parameters, ()=> {/*Postprocessor A logic */}};

processPostprocessorA({ verb: "get", handler: ()=> {...} });
user1514042
  • 1,899
  • 7
  • 31
  • 57