As a Front-end Engineer, 10 Useful JavaScript Coding Techniques That You Should Use
Preface
In the past, I wrote a lot of junk code, and now that looks terrible.
When I saw those code fragments again, I even doubted whether I was suitable to be a programmer.
So, here are 10 JavaScript tips to help you avoid writing the kind of junk code I once did.
1. Promise callback hell
Promises provide an elegant way to handle asynchronous operations in JavaScript. This is also one of the solutions to avoid “callback hell”. But I didn’t really understand what it meant, so I wrote this code snippet.
I did these things:
- Get the basic information of the user first.
- Get a brief summary of all articles by user information.
- Get article details through articles briefly.
// ❌
getUserInfo()
.then((userInfo) => {
getArticles(userInfo)
.then((articles) => {
Promise.all(articles.map((article) => getArticleDetail(article)))
.then((articleDetails) => {
console.log(articleDetails)
})
})
})