Cannot have more than 1 await

function fakeRequest(url) {
    return new Promise((resolve, reject) => {
        delay = Math.floor(Math.random() * 4500) + 500;
        setTimeout(() => {
            if (delay > 4500) {
                resolve(url + ": success")
            }
            else {
                reject(url = ": error")
            }
        }, delay);
    })
  })
}

async function makeTwoRequests() {
    let data1 = await fakeRequest("/page1");
    console.log("Data 1:", data1)
    let data2 = await fakeRequest("/page2");
    console.log("Data 2:", data2)
}
makeTwoRequests()

When I remove data2 or data1 and just have 1 await it works. But when there are multiple awaits it doesn’t work and says: Uncaught (in promise) : error

Thanks!

First of all, you have an extra curly brace and parenthesis in the fakeRequest function you posted above.

Second of all, you have to run the await in a try/catch so that you handle any errors that may occur. This is what that error is telling you. The way you have the randomness set up, most of the time it is going to fail and throw the error.

1 Like

Okay, removed them.

Okay:

    try {
        let data1 = await fakeRequest("/page1");
        let data2 = await fakeRequest("/page2");
        console.log(data1)
        console.log(data2)
    }
    catch {
        console.log("There was an erro")
    }

Oh yea, I wanted it to be the other way around lol.
Thanks!