Javascript: External callback function

Created on 22 Jan 2017  路  3Comments  路  Source: airbnb/javascript

_The following examples are based (but no dependent) on Express router._

Considering the following correct case:

8.1 When you must use function expressions (as when passing an anonymous function), use arrow function notation.

router.get('/url', (req, res) => {
  ...
});

Should you?

Write the callback function as an arrow function as well, if called from an external place?

// controllers.js
export const getUrlController = (req, res) => {
  ...
});
// routes.js
import { getUrlController } from './controllers'

router.get('/url', urlController);

Or should it rather be a function in this case?

// controllers.js
export const getUrlController = function getUrl(req, res) {
  ...
};
question

Most helpful comment

Arrow functions don't have names, and are really only used best as inline callbacks. I'd recommend using a proper named function in this case.

All 3 comments

Arrow functions don't have names, and are really only used best as inline callbacks. I'd recommend using a proper named function in this case.

@ljharb Do you think it is worth clarifying this in the style guide? I think in general the use of functions vs named arrow functions in the global namespace isn't clear from the current airbnb style guide.

// good
function parseSchema(schema) {
  ...
}

// bad
const parseSchema = (schema) => {
  ...
};

There is some justification on reasoning at https://stackoverflow.com/a/23045200/325365.

While I agree with that, I don't think that's something that has enough internal consensus at airbnb to warrant inclusion in the guide.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

tpiros picture tpiros  路  3Comments

danielfttorres picture danielfttorres  路  3Comments

mismith picture mismith  路  3Comments

brendanvinson picture brendanvinson  路  4Comments

weihongyu12 picture weihongyu12  路  3Comments