4 ms·
These tips are all great. Thanks for the article. My 2 cents from working over 5 years with NodeJS is to be careful when you sacrifice code readability over pe
by drinchev 9y ago
These tips are all great. Thanks for the article.
My 2 cents from working over 5 years with NodeJS is to be careful when you sacrifice code readability over performance.
`.forEach`, `.map`, `.filter` are way more readable / maintainable than a 20 lines `for loop` with n+ var assignments.
As for `try..catch`, I use ( readability - again ) to follow a Promise chain.
return new Promise( resolve => resolve( JSON.parse( req.body ) ) )
.then( body => db.update( body ) )
.catch( err => /* will catch JSON.parse as well as db.update */ )
- 11235813213455 9y agoI'd put this logic in db.update, and avoid new Promise as well (I know Promise.resolve().then(() => JSON.parse( req.body )) takes one more tick, there's a Promise.try proposal too), but just: update: async body => { const {id, name} = JSON.parse(body) // throws SyntaxError if not valid