-4

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 ?

  • 1
    Duplicate of https://stackoverflow.com/questions/34361379/are-arrow-functions-and-functions-equivalent-interchangeable ? – T.J. Crowder Oct 15 '21 at 09:59

2 Answers2

2

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",
      }];
    });
  }
}
Olian04
  • 6,480
  • 2
  • 27
  • 54
0

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.

jmartinezmaes
  • 359
  • 1
  • 8