bevacqua / js

:art: A JavaScript Quality Guide
https://ponyfoo.com
MIT License
2.93k stars 502 forks source link

Question #24

Closed rmrewak closed 8 years ago

rmrewak commented 8 years ago

@bevacqua Thanks for the amazing work done.

Use Function() as a "no-op".

function (cb) {
  setTimeout(cb || Function(), 2000);
}

why not use if-else and do not call setTimeout at all ?

bevacqua commented 8 years ago

Say you have an external API method with a opts, done? signature. That method internally calls many other functions, some of which are asynchronous as well. If you set done = done || Function() or some such at the entry point, you won't have to check whether a callback is being passed in the internal functions.