Skip to content

Instantly share code, notes, and snippets.

@googya
Last active August 10, 2020 09:31
Show Gist options
  • Save googya/75f83217ed19aeb01b571a498c07367b to your computer and use it in GitHub Desktop.
Save googya/75f83217ed19aeb01b571a498c07367b to your computer and use it in GitHub Desktop.
const fs = require('fs');
function check_result() {
return fs.existsSync('foo.txt');
}
function* infiniteSequence() {
let num = 1
while (true) {
yield num
num = num * 2
}
}
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve => {
resolve(fs.existsSync("foo.txt"))
}, ms));
}
async function delayedChecking() {
let res = check_result();
console.log(`result is ${res}`)
while (!res) {
let z = x.next().value;
console.log(`check ${z} milliseconds later`)
await sleep(z);
res = check_result()
}
}
let x = infiniteSequence()
delayedChecking();
@googya
Copy link
Author

googya commented Aug 10, 2020

const fs = require('fs');

function check_result() {
    return fs.existsSync('foo.txt');
}

function* infiniteSequence() {
    let num = 1
    while (true) {
        yield num
        num = num * 2
    }
}

function sleepAndCheck(ms) {
    return new Promise((resolve, rejected) =>
        setTimeout(() => {
            resolve(check_result())
        }, ms)
    );
}

async function delayedChecking() {
    let res = check_result();
    console.log(`result is ${res}`)
    while (!res) {
        let z = x.next().value;
        console.log(`check ${z} milliseconds later`)
        res = await sleepAndCheck(z);
        console.log(`result is ${res}`)
    }
}

let x = infiniteSequence()
delayedChecking();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment