promise

function a(){
        console.log('A');
}
function b(){
    console.log('B');
}
a();
b();

//A
//B
function a(){
    setTimeout(function(){
        console.log('A');
    },1000);
}
function b(){
    console.log('B');
}
a();
b();

//B
//A
//콜백지옥
function a(callback){
    setTimeout(function(){
        console.log('A');
        callback();
    },1000);
}
function b(callback){
    setTimeout(function(){
        console.log('B');
        callback();
    },1000);
}
function c(callback){
    setTimeout(function(){
        console.log('C');
        callback();
    },1000);
}
function d(callback){
    setTimeout(function(){
        console.log('D');
    },1000);
}
a(function(){
    b(function(){
        c(function(){
            d()
        });
    });
});

//A
//B
//C
//D
//promise
function a(){
    return new Promise((resolve, reject) => {
        setTimeout(()=>{
            console.log("A");
            resolve();
        },1000);
    });
}
function b(){
    return new Promise((resolve, reject) => {
        setTimeout(()=>{
            console.log("B");
            resolve();
        },1000);
    });
}
function c(){
    return new Promise((resolve, reject) => {
        setTimeout(()=>{
            console.log("C");
            resolve();
        },1000);
    });
}
function d(){
    return new Promise((resolve, reject) => {
        setTimeout(()=>{
            console.log("D");
            resolve();
        },1000);
    });
}
// a().then(()=>{
//     return b()
// }).then(()=>{
//     return c()
// }).then(()=>{
//     return d()
// })
a()
.then(()=> b())
.then(()=> c())
.then(()=> d())
//promise 기본문법
function a(){
    return new Promise((resolve, reject) => {
        if(isError){
            reject()
        }
        setTimeout(()=>{
            console.log("A");
            resolve();
        },1000);
    });
}
a().then()
    .catch((error) => {
        console.log(error)
    })

Last updated

Was this helpful?