vscode-nodejs-repl icon indicating copy to clipboard operation
vscode-nodejs-repl copied to clipboard

Investigate why the following code won't run

Open lostfields opened this issue 7 years ago • 5 comments

    let promises = [
        Promise.resolve(1),
        Promise.reject('dada'),
        Promise.resolve(3)
    ]

    (async () => {
        try {
            let results = await Promise.all(promises);

            console.log(results.join(','))
        }
        catch(ex) {
            console.log(ex)
        }
    })()

lostfields avatar Jun 20 '18 07:06 lostfields

async function* fetch() {
    let page = 1, lastPage = 5;

    while(page < lastPage)
        yield await Promise.resolve(page++) // async http request maybe?
}

(async () => {

    for await(let page of fetch()) {
        console.log('page ' + page)
    }

})()

lostfields avatar Aug 23 '18 10:08 lostfields

let promises = [
    Promise.resolve(1),
    Promise.reject('dada'),
    Promise.resolve(3)
]

(async () => {
    try {
        let results = await Promise.all(promises);

        console.log(results.join(','))
    } catch(ex) { // `catch` need following the right brace `}`
        console.log(ex) // output: 'dada'
    }
})()

plylrnsdy avatar Nov 25 '18 12:11 plylrnsdy

great, thank you @plylrnsdy. Guess I'm not into javascript anymore, it's allowed in typescript :-)

the last one with the async generator may be that it isn't supported in Node 8.9? Or I'm missing how to augment asyncIterator to Symbol.

lostfields avatar Nov 25 '18 20:11 lostfields

for await isn't supported in 8.9 - either rewrite with helpers or parse through ts-node

https://jakearchibald.com/2017/async-iterators-and-generators/ https://medium.com/@segersian/howto-async-generators-in-nodejs-c7f0851f9c02

lostfields avatar Nov 26 '18 08:11 lostfields

I think the extension can use child process to create a new Node process as REPL with user's Node.js, so that we can use Node.js which the version user had.

plylrnsdy avatar Nov 26 '18 08:11 plylrnsdy