0

I have a function where it passes two params to other function (one of them is function name):

sendSomething(){
this.responseWithModal(this.responseType, this.openModal());
}

responseWithModal(response, successFunction){
this.store.dispatch(sendResponse(payload: {something}));
successFunction;
}

successFunction(){
window.open("www.somelink.com");
}

When I am running npm run lint it can not pass linting and gets error:

unused expression, expected an assignment or function call

The thing is that everything works as expected, but I can not pass lint because of that fail for successFunction; call in responseWithModal() function. Maybe there is better solution how to solve function as parameter calling?

me_gone
  • 9
  • 3

1 Answers1

0

You have two problems, which aren't necessarily immediately obvious because you're not using types, which you should.

The first issue is that

sendSomething(){
  this.responseWithModal(this.responseType, this.openModal());
}

should be

sendSomething(){
   this.responseWithModal(this.responseType, this.openModal);
}

i.e., you shouldn't invoke the function here, just pass the function to the method

Then,

responseWithModal(response, successFunction){
  this.store.dispatch(sendResponse(payload: {something}));
  successFunction;
}

should be

responseWithModal(response, successFunction){
  this.store.dispatch(sendResponse(payload: {something}));
  successFunction();
}

i.e. you should invoke the function here

GreyBeardedGeek
  • 29,460
  • 2
  • 47
  • 67
  • That was the first try for implementing such functionality. The thing is that after such call, all variables and observables initialized during ngOnInit() are marked as undefined and in such case I get error that I am using undefined values in my mentioned successFunction() call. – me_gone Dec 05 '19 at 18:47
  • Sounds like you have other issues elsewhere, and this is just a symptom. – GreyBeardedGeek Dec 05 '19 at 23:37
  • Yes, when passing function as parameter, I was not binding scope to the function I pass `this.openModal.bind(this)` – me_gone Dec 07 '19 at 09:35