lukassteiner
lukassteiner

Reputation: 847

Organize helper in sails.js

Is there a way to structure code in a helper in sails.js v1.0?

Currently I have something like that:

module.exports = {

  friendlyName: 'Example helper that does three things',

  description: '',

  inputs: {},

  exits: {},

  fn: async function (inputs, exits) {
    const doFirstThing = function () {
      // do something
    };

    const doSecondThing = function () {
      // do something
    };

    const doThirdThing = function () {
      // do something
    };

    doFirstThing();
    doSecondThing();
    doThirdThing();
  },
};

The three functions doFirstThing doSecondThing doThirdThing each have about 15 lines of code. Right now, the code is pretty hard to read. Is there a way to put there functions below the fn function or structure it in any other way that is more readable?

Upvotes: 1

Views: 1122

Answers (1)

Chirag Ravindra
Chirag Ravindra

Reputation: 4830

You can always define the function outside and assign it to your module.exports object later. You can also define doFirstThing and the other two functions separately if they do not use the closure variables of the doEverything function

async function doEverything(inputs, exits) {
    const doFirstThing = function () {
        // do something
    };

    const doSecondThing = function () {
        // do something
    };

    const doThirdThing = function () {
        // do something
    };

    doFirstThing();
    doSecondThing();
    doThirdThing();
}
module.exports = {

    friendlyName: 'Example helper that does three things',

    description: '',

    inputs: {},

    exits: {},

    fn: doEverything
};

Upvotes: 2

Related Questions