Skip to content Skip to sidebar Skip to footer

Generator Not Evaluating Promises

Why doesn't the generator wait for the asynchronous prm promise to complete before moving on to the next yield? function *verify() { try { let prm = new Promise((resolv

Solution 1:

Because a generator - on its own - doesn't wait for anything. It just emits what you were yielding from the next() call. You could of course have your loop wait when before calling next again when it gets a promise.

What you are thinking of is async/await. You want to write

asyncfunctionverify() {
    try {
        let prm = newPromise((resolve,reject) => {
            resolve("abc");
        })
        let k = await prm
        console.log(k)
        console.log("1")
        console.log("2")
        console.log("3")
    } catch (err) {
        console.log("error")
    }
}

var prm = verify();

Post a Comment for "Generator Not Evaluating Promises"