Try adding an await keyword before the first method(the method that generates the cookie)
try {
// Wait for method1 to finish
await method1();
// Then run method2
await method2();
} catch (error) {
console.error("An error occurred:", error);
}