0

I am trying to pause the execution of the function until the value of the global variable changes. But it doesn't work, after waiting, the function did not continue its work. Where is mistake?

let currentFunction = 1;

const sleep = () => {
    currentFunction += 1;
    console.log(currentFunction);
    return new Promise((resolve) => {
        if (currentFunction === 5) {
            return resolve();
        }
        return setTimeout(sleep, 1000);

    });
};

const first = async () => {
    console.log('Hello');
    await sleep();
    console.log('now here');
};

first();
Barmar
  • 596,455
  • 48
  • 393
  • 495

0 Answers0