Home > other >  Safe to remove async when no await's are used?
Safe to remove async when no await's are used?

Time:10-30

This may seam as a stupid question, but can I remove the async function below since there are no await's? It is a part of a large code base that is in production, so I am wondering if there could be some weird side effects if I remove async?

(async function(){
  'use strict';
  const {appLogger, monitorLogger} = require('./functions/logger');

...

  for (let i in p) {
    p[i].Filename = i;
    check(p[i]);
  }

...
    
  app.get('/example.json', (req, res) => {
    res.send(JSON.stringify(checkStates, null, 4));
  })

  app.listen(port, () => {
    appLogger.info(`Exposing http://localhost:${port}/example.json`);
  })

})();

CodePudding user response:

My only thought would be that the await wraps any returned value (that isn't a promise) in a promise. However, this is a self-calling function and from what you've shown us there is nothing returned and nothing expecting a return. So unless there's other code you haven't shown, yes you should be fine.

CodePudding user response:

Yes in this case you could remove async.

CodePudding user response:

nope you dont need async if you're not awaiting for any promises.

CodePudding user response:

Yes there might be side effects. async makes the function returns a promise, and that may have impacts somewhere, there's not always strictly the same behavior before and after removing the async keyword, for example

const fn = () => 10
const afn = async () => 10

afn() // a Promise
fn() // a number

afn().then(() => {}) // ok
fn().then(() => {}) // throws :(

but in your case, all good, this won't change anything

  • Related