This is a question related to JavaScript promise.

Asked 2 years ago, Updated 2 years ago, 100 views

function delay(a) {
    setTimeout(() => {
        console.log(a);
    }, 1000);
}

function prompt(a) {
    console.log(a);
}

var temp = function() {
    return new Promise((resolve) => {
        delay('first');
        resolve();
    });
};

temp().then(() => {
    prompt('second');
});

I want to print it out in the order of first and second.

The output starts from the second.

Can we print it out in first second order without modifying the delay() and promt() functions?

javascript promise

2022-09-21 11:18

1 Answers

function delay(a, resolve) {
    setTimeout(() => {
        console.log(a);
        resolve();
    }, 1000);
}

function prompt(a) {
    console.log(a);
}

var temp = function() {
    return new Promise((resolve) => {
        delay('first', resolve);
    });
};

temp().then(() => {
    prompt('second');
});


2022-09-21 11:18

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.