I am writing a Web Extension that attempts to fetch pages from a session based API in parallel. The API uses cookies to store the session information. So, the following sequence of commands would work:
// Fetch sequence 1
await fetch("https://url.endpoint/start_session");
const usefulData1 = await fetch("https://url.endpoint/navigate_to_X",
{ credentials: "include" });
// Fetch sequence 2
await fetch("https://url.endpoint/start_session");
const usefulData2 = await fetch("https://url.endpoint/navigate_to_Y",
{ credentials: "include" });
I would like to run Fetch sequence 1 and Fetch sequence 2 in parallel. The problem is, every time I access https://url.endpoint/start_session I get sent back a new set of cookies, and all fetch requests with { credentials: "include" } use the currently set cookies (which get overridden when start_session returns).
My question is: is there a way to set up multiple "sessions" so that these requests can run in parallel with their own cookies? Or is there some other workaround? (It appears the fetch api will not allow you to get or set the Set-Cookie header manually...)
If you want to await the completion of two promises at once, you can use Promise.all. This will create a new promise that only resolves when all promises passed in to it complete, but still lets them run in parallel. In this case, I would await the start_session endpoint, then await a Promise.all of the other two, like this:
await fetch("https://url.endpoint/start_session");
const [usefulData1, usefulData2] = await Promise.all([
fetch("https://url.endpoint/navigate_to_X", { credentials: "include" }),
fetch("https://url.endpoint/navigate_to_Y", { credentials: "include" })
])