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(){
    setTimeout(function(){
        console.log("d");
    },1000);
}
a(function(){
    b(function(){
        c(function(){
            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(() => b())
.then(() => c())
.then(() => d())
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