코드 냄새 78 - 콜백 지옥
8615 단어 nodeoopjavascripttutorial
TL;DR: Don't process calls in a callback way. Write a sequence.
문제
솔루션
샘플 코드
잘못된
var fs = require('fs');
var fileWithData = '/hello.world';
fs.readFile(fileWithData, 'utf8', function(err, txt) {
if (err) return console.log(err);
txt = txt + '\n' + 'Add Data!';
fs.writeFile(fileWithData, txt, function(err) {
if(err) return console.log(err);
console.log('Information added');
});
});
오른쪽
var fs = require('fs');
function logTextWasAdded(err) {
if(err) return console.log(err);
console.log('Information added');
};
function addData(error, actualText) {
if (error) return console.log(error);
actualText = actualText + '\n' + 'Add data';
fs.writeFile(fileWithData, actualText, logTextWasAdded);
}
var fileWithData = 'hello.world';
fs.readFile(fileWithData, 'utf8', addData);
발각
이 문제는 육안으로 볼 수 있습니다. 많은 린터가 이러한 복잡성을 감지하고 경고할 수 있습니다.
태그
var fs = require('fs');
var fileWithData = '/hello.world';
fs.readFile(fileWithData, 'utf8', function(err, txt) {
if (err) return console.log(err);
txt = txt + '\n' + 'Add Data!';
fs.writeFile(fileWithData, txt, function(err) {
if(err) return console.log(err);
console.log('Information added');
});
});
var fs = require('fs');
function logTextWasAdded(err) {
if(err) return console.log(err);
console.log('Information added');
};
function addData(error, actualText) {
if (error) return console.log(error);
actualText = actualText + '\n' + 'Add data';
fs.writeFile(fileWithData, actualText, logTextWasAdded);
}
var fileWithData = 'hello.world';
fs.readFile(fileWithData, 'utf8', addData);
이 문제는 육안으로 볼 수 있습니다. 많은 린터가 이러한 복잡성을 감지하고 경고할 수 있습니다.
태그
결론
콜백 지옥은 미래나 약속이 있는 프로그래밍 언어에서 매우 흔한 문제입니다.
콜백은 점진적으로 추가됩니다. 초반에는 별 무리가 없습니다.
리팩토링이 없는 복잡성은 읽고 디버깅하기 어렵게 만듭니다.
처지
코드 냄새 06 - 너무 영리한 프로그래머
Maxi Contieri ・ 2020년 10월 25일 ・ 2분 읽기
#codenewbie
#tutorial
#beginners
There are two ways to write code: write code so simple there are obviously no bugs in it, or write code so complex that there are no obvious bugs in it.
토니 호어
소프트웨어 엔지니어링 좋은 인용구
Maxi Contieri ・ 12월 28일 '20 ・ 13분 읽기
#codenewbie
#programming
#quotes
#software
이 기사는 CodeSmell 시리즈의 일부입니다.
코드에서 냄새 나는 부분을 찾는 방법
Maxi Contieri ・ 2021년 5월 21일 ・ 4분 읽기
#codenewbie
#tutorial
#codequality
#beginners
Reference
이 문제에 관하여(코드 냄새 78 - 콜백 지옥), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다
https://dev.to/mcsee/code-smell-78-callback-hell-287f
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)
코드 냄새 06 - 너무 영리한 프로그래머
Maxi Contieri ・ 2020년 10월 25일 ・ 2분 읽기
#codenewbie
#tutorial
#beginners
There are two ways to write code: write code so simple there are obviously no bugs in it, or write code so complex that there are no obvious bugs in it.
토니 호어
소프트웨어 엔지니어링 좋은 인용구
Maxi Contieri ・ 12월 28일 '20 ・ 13분 읽기
#codenewbie
#programming
#quotes
#software
이 기사는 CodeSmell 시리즈의 일부입니다.
코드에서 냄새 나는 부분을 찾는 방법
Maxi Contieri ・ 2021년 5월 21일 ・ 4분 읽기
#codenewbie
#tutorial
#codequality
#beginners
Reference
이 문제에 관하여(코드 냄새 78 - 콜백 지옥), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://dev.to/mcsee/code-smell-78-callback-hell-287f텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)