I have a basic understanding of arrow functions. I however came across this piece of code and am some what stumbled.
const errors = require('@feathersjs/errors');
const test = () => async context => {
//omited
}
module.exports = {
before: {
all: [],
find: [],
get: [],
create: [test()],
update: [],
patch: [],
remove: []
},
};
What exactly is this line doing ?
const test = () => async context =>
It looks like some form of double arrow functions?
I am including the hooks code as it is part of the library feathersjs and this function is writing a hook before a particular call.
If you are not passing curly braces, then the arrow function by default returns the value you're pointing it to. So think of it this way,
let testValue = () => 'some value';
is the same as
let testValue = () => {
return 'some value';
};
So your function can be written as
let test = () => {
return async context => {
// some process
}
};
Basically the function is returning another function, this is called Currying and is a way of creating higher order functions and helps avoid repeatedly passing some values. For example, if I have a helper function which helps me create a multiplier
let multiplier = (x) => (y) => x * y;
let multiplyBy5 = multiplier(5);
console.log(multiplyBy5(5)); // 25
I basically created a function multiplyBy5 which creates a function to multiply my values by 5, and I can create more multipliers for different values.