Posts /

Call-back hell이란?

18 Apr 2017

Call-back hell의 개념

asyncfunction(params,function(){
    asyncfunction(params,function(){
        asyncfunction(params,function(){
            asyncfunction(params,function(){
                asyncfunction(params,function(){
                    asyncfunction(params,function(){
                    });
                });
            });
        });
    });
});         

실제 코드를 통한 예제

var fs = require('fs');
var src = '/tmp/myfile.txt';
var des = '/tmp/myfile_async.txt';

fs.readFile(src,'utf-8',function(err,data) {
    console.log(data);
    if (err) {
        console.log("Read file error");
    } else {
        console.log("Read file is done");
        fs.writeFile(des,data,function(err) {
            if (err) {
                console.log("Write file error");
                return;
            }
            console.log("Write file is done");
        });
    }
});

Twitter Facebook Google+