본문 바로가기
카테고리 없음

Refactoring javascript 1

by tricks 2020. 9. 23.

Clean coding in JavaScript

리팩토링 자바스크립트, 클린 코드 작성하기

Using the map function

before


const myList = list => {

const newList = []

for (let i = 0; list.length; i++) {

newList[i] = list[i] * 2

}

return newList

}

after


const myList = list => list.map(x => x * 2)

Avoid callback hell with async/await

before


const sendToEmail = () => {

getIssue().then(issue => {

getOwner(issue.ownerId).then(owner => {

sendEmail(owner.email, `some text ${issue.number}`).then(() => {

console.log("email successfully")

})

})

})

}

after


const sendToEmail = async () => {

const issue = await getIssue()

const owner = await getOwner(issue.ownerId)

const email = await sendEmail(owner.emaill, `some text ${issue.number}`)



console.log("email successfully")

}

댓글