1

I'm using an arrow function to pass a parameter to some other function. It looks sort of like this:

someFunction(parameter1, () => {
  return model.associatedGroups.filter(group => {
    return group.isAssociated === true;
   })
}, parameter3)

but when I'm debugging it, I'm receiving a function on the method i'm calling instead of a filtered array. How should I write it in order to get back a filtered array?

4
  • An arrow function is a function. You don't get the filtered array until someFunction calls the callback. Commented Oct 13, 2016 at 9:36
  • Why are you using an arrow function if you don't want it to be a function? Commented Oct 13, 2016 at 9:42
  • but I guess there could be some cases were you could use an arrow function to return value, and it will actually make sense, no? in these cases you would IIFE your function in order to get the value? Commented Oct 13, 2016 at 9:45
  • The only case would be if you're using this in the function, and you're using an arrow function to pass along the current value of this. Commented Oct 13, 2016 at 9:46

2 Answers 2

1

You are just passing reference to a function.

Call the function in place to pass its result as the argument:

someFunction(
  parameter1, 
  (() => model.associatedGroups.filter(group => group.isAssociated === true))(), 
  parameter3
)

Or just pass the result of the filter:

someFunction(
  parameter1, 
  model.associatedGroups.filter(group => group.isAssociated === true), 
  parameter3
)

Or pass the function as you are already and call it within someFunction to get its result.

Sign up to request clarification or add additional context in comments.

2 Comments

There's no point in using an IIFE in an argument like that. Just call model.associatedGroups.filter() in the argument.
@Barmar u'r absolutely right... How did I not see it??
0

It's because you pass a function, not an array. If you want the result of this function you have to call it.

Moreover with arrow function you don't need to put `return if you have only one line.

Try with this:

someFunction(parameter1, (() => model.associatedGroups.filter(group => group.isAssociated === true))(), parameter3)

Edit : this is enought :)

someFunction(parameter1, model.associatedGroups.filter(group => group.isAssociated === true), parameter3)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.