Home > OS >  How to change this arrow function in Typescript
How to change this arrow function in Typescript

Time:10-16

I need to change this arrow function to a regular function on the line of my return toto but i don't really understand how, here is the code :

class Ratchet extends APlugin<CentralizerConfigPluginRatchet> {
  public search = (): Promise<CentralizerNotif[]> => {throw Error("Not implemented");};

  public notif =
  (): Promise<CentralizerNotif[]> => {
    const toto = new RatchetLib.RatchetAPI(this.config.baseurl);
    return toto.getAsset({validation: true}).then(result => {
      return [{
        name: `You ${result.assets.length} things to moderate `,
        link: "https://www.exemple.com",
      }];
    });
  };
}

Anyone can help me please ?

CodePudding user response:

I see no reason as to why you would need to change it to a keyword function. But if you wan't to you just have to remove the = and the =>:

class Ratchet extends APlugin<CentralizerConfigPluginRatchet> {
  public search(): Promise<CentralizerNotif[]> {
    throw Error("Not implemented");
  }

  public notif(): Promise<CentralizerNotif[]> {
    const toto = new RatchetLib.RatchetAPI(this.config.baseurl);
    return toto.getAsset({validation: true}).then(result => {
      return [{
        name: `You ${result.assets.length} things to moderate `,
        link: "https://www.exemple.com",
      }];
    });
  }
}

CodePudding user response:

Changing an anonymous arrow callback function into a regular function is straightforward. In your case,

return toto.getAsset({validation: true}).then(result => {
  /* ... */
});

will become

return toto.getAsset({validation: true}).then(function (result) {
  /* ... */
});

You can also choose to have a named function here:

return toto.getAsset({validation: true}).then(function myCustomFunc(result) {
  /* ... */
});

Keep in mind that arrow functions and function declarations (using the function keyword) have some differences to take note of.

  • Related