This is a question related to JavaScript promise.

Asked 2 years ago, Updated 2 years ago, 56 views

var temp = function(){
    return new Promise(
        (resolve, reject)=>{
            setTimeout(
                ()=>{console.log(1)}
                ,1000)
        }
    )
}


temp().then(
    ()=>{
        console.log(2)
    }
)

I'm practicing my promise.

Wait 1 second and print out 1, then print out.

Why can't I print out 1 and 2?

javascript promise

2022-09-21 11:18

1 Answers

var temp = function() {
    return new Promise((resolve, reject) => {
        setTimeout(() => {
            console.log(1);
            resolve();
        }, 1000)
    });
}

temp().then(() => {
    console.log(2)
});

resolve() is missing.

Note: https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Promise/resolve


2022-09-21 11:18

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.