How to pass argument to the function without parentheses?

Related searches

I have the following code:

this.somePromiseFn<T>  // this fn is actually a promise
.then(res => res as T)
.catch(err => this.handleError<T>(err));

handleError<T>(err: HttpErrorResponse) {
  // do something here
  // and return like
  return {} as T;
}

The above code works fine. But how I can pass the err argument directly to the handleError method without using another callback function. As catch by default passes the err to the callback function, it should also pass the err to the handleCatchError. I want something like:

this.somePromiseFn<T>
.then(res => res as T)
.catch(this.handleCatchError<T>); // this line gives error 'An argument for 'err' was not provided'

But the above code produces the error saying:

Expected 1 argument, but got 0. An argument for 'err' was not provided.

Though I have visited the following question already:

  1. addEventListener calls the function without me even asking it to
  2. Javascript "addEventListener" Event Fires on Page Load

But the above questions also suggest using another callback function.

this.somePromiseFn<T>
.then(res => res as T)
.catch(this.handleCatchError<T>);

this should work, tried to reproduce the error but it's working see a demo, might be I'm missing something, check and update if necessary.

Javascript: call functions without using parentheses (what , Passing Functions –. You can pass a function as an argument by creating the reference, calling the function without parentheses, and provide it� And, when we code subfunction without parentheses in a return statement, the main function passes the sub-function reference to the callable rather than executing it. Here the callable decides what to do with the reference.

Based on stackblitz provided by you, see

this.someFn().then(r => console.log(r)).catch(err => this.cb<any>(err));

Python, by the new operator. The parentheses are optional when using this operator, hence the function would be invoked even without them. It can be used with parameters bypassing them in the literals. To invoke a function,� Other examples of passing a function as an argument. The key argument accepted by sorted, min, and max is just one common example of passing functions into functions. Two more function-accepting Python built-ins are map and filter. We’ve already seen that filter will filter our list based on a given function’s return value.

Don't give generated type and bind "this":

this.somePromiseFn<T>
    .then(res => res as T)
    .catch(this.handleCatchError.bind(this));

How to invoke a function without parentheses using JavaScript , Without parentheses you're not actually calling the function. Notice that when I passed in the compareNums function as an argument to the� R assigns the function code of signif to FUN, so now FUN () is a perfect copy of signif () and works exactly the same way. R takes the argument digits and passes it on to FUN (). Note the absence of parentheses in the argument assignment.

Why do we call the functions without parentheses ie, call a method without parenthesis because I figure it doesn't have any parameters Using no () means to fetch the function to be passed along as a callback. Omit the parentheses, list the arguments, and do not assign the function to a variable, as shown in the following example. VB. MsgBox "Task Completed!", 0, "Task Box". If you include parentheses in the preceding example, the statement causes a syntax error.

calling a method with parenthesis vs. no-parenthesis? whats the diff , Parameters and Arguments. Information can be passed to functions as a parameter. Parameters act as variables inside the function. Parameters are specified after the function name, inside the parentheses. You can add as many parameters as you want, just separate them with a comma:

This creates a function taking one argument x and returning the value of the polynomial x^2 + 2x - 1 at that value. Notice that the result is a generic function, but with a compiler-generated name based on consecutive numbering. The primary use for anonymous functions is passing them to functions which take other functions as arguments.

Comments
  • This doesn't seem to be a minimal reproducible example, unless you count the stackblitz link in a comment on one of the answers. Does this.someFn().then((r) => console.log(r)).catch(this.cb); work for you? If not, please edit this question to constitute a reproducible issue. Good luck!
  • Your code is working because you don't have a type variable in your code. See here, it is not working..!
  • Thanks for your answer, but I am already using the solution you provided..! But I wanted another (sorter) way..!
  • Thanks for your answer, but the type variable is required..!
  • If you don't call handleCatchError<T>() function, you can't give generic type.