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');
        callback();
    },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())

//A
//B
//C
//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?