简体   繁体   中英

Typescript empty callback function

In my angular project i have a method that takes two callback functions, one for success one for fail which currently looks like:

somefunction(function(token) {
    console.log('Got token: ' + token);
  }, function(error) {
    console.log('Failed to get token', error);
});

Lets say I want to keep the error handling but im not interested in the success callback, what is the proper way to still handle the error callback? Should I create an empty success method like:

somefunction(function(token) {
  }, function(error) {
    console.log('Failed to get token', error);
});

should i give null as first parameter?

somefunction(null, function(error) {
    console.log('Failed to get token', error);
});

Im not sure if there are any dangers of using null and I should just add an empty function (which looks redundant).\\

edit: the function is from a library, I prefer to not touch the library.

I will try to give a more in-depth explanation of my comment. What you are doing seams to be an old school JavaScript coding the type of coding TypeScript was invented to prevent. In JavaScript you have two type of "empty" values that are similar but not the same null and undefined. For a while now in TypeScript all values are non nulluble by default that means unlike in JavaScript you will get a compiler error if you try to put null or undefined in a function. So if your somefunction looks like this.

function someFunction(success: (token: string) => void, error: (error:string) => void) {

}

A call like somefunction(null, (e) =>{}) will not compile at all because everything is not nullable by default. And you must give it an empty callback like somefunction(()=>{}, () =>{}) you can see that you don't event need the token parameter because any paraatreless function can be cast to a parameter function. If the creator of some function allowed "null" in type script he would have defined like so

function someFunction(success?: (token: string) => void, error?: (error:string) => void) {

}

Where the ? is a shorter way of writing () => void | undefined. Now you can call it with someFunction(undefined, ()=>{}); but a call someFunction(null, ()=>{}); will produce an error since null is not equal (===) undefined. This is the most common way to allow empty calls to functions in TypeScript. If you really want a null value you can define somefunction like so

function someFunction(success: ((token: string) => void) | null, error: ((error:string) => void) | null) {

    }

This is really uncommon in typescript and way two if what most people use. But since you asked about "best practices" from what I can see what you are doing is some async workflow and modern JavaScript and typescript tend to preferer Promises and in angular Observables(The two are not mutually exclusive and can work well together).

If you prefer to use a much more ES6 way, you could wrap this function into a promise. This will allow you to use the function then and catch on this function.

A simple way to do this is to create a new Promise and pass the resolve and reject as parameter to the function.

let myPromise = new Promise((resolve, reject) => {
    somefunction((item) => { resolve(item) }, (error) => { reject(error) });
   // or even somefunction(resolve, reject);
});

You can then use this promise as any other promise. In your case ;

myPromise.catch((error) => {
    console.error(error);
});

This seams far fetch for what you are trying to do, but you could wrap this into a function and use it everywhere you have to use the library function.

function toPromise(fnc) {
    return new Promise((resolve, reject)  => fnc(resolve, reject));
}

toPromise(somefunction).catch((error) => {
   //handle the error
});

Here i have use only arrow function, but you could use normal callback function if you'd like.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM