question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Question] Adding an if condition to Promise.all

See original GitHub issue

I am trying to do the following: When I am on the cart page and click the CHECKOUT button, then there are 2 things that can happen: I am redirected to the checkout page I am asked sometimes to log in again => this is a bug in the app since I am already logged in but it asks me to log in again for some reason. I need to write a Playwright test for the checkout process. And in this test, I need to support that buggy behavior since there is no indication it will be resolved any time soon (or whatsoever).

This is what I got so far:

async clickCheckoutBtn() {
    if (this.#isGuest) {
      // redirects to the login page, so we don't have waitForURL like when we are logged in
      await this.#checkoutBtn.click();
    } else {
      // TODO add here somehow to login again if required after clicking the checkout button
      await Promise.all([
        this.page.waitForResponse(
          (response) =>
            response.url().includes('/my-account/addressform') &&
            response.status() === ResponseCode.SUCCESS
        ),
        
        this.#checkoutBtn.click(),
        this.page.waitForURL(/.*\/delivery-address\/add$/),
      ]);
    }
  }

Now, this is what I would like to do:

    await page.getByRole('button').click();
    if(await page.getByPlaceholder('login').isVisible()) {
      await loginPage.login(username, password);
    } 
    await page.waitForURL(/.*\/delivery-address\/add$/),  

But in the case when I am redirected to the checkout I also need to waitForResponse to the /my-account/addressform. (so after logging in I need to wait for it, but also after direct redirect to the checkout page, I need to wait for it as well) So I would like to do this somehow inside the Promise.all, but I don’t how to do it. Can you please help?

Issue Analytics

  • State:closed
  • Created 10 months ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
marerucommented, Dec 10, 2022

@aslushnikov Thanks for the help, it was very useful in the end. Sorry, sometimes I cannot distinguish what should go where - is it Typescript I am wrestling with or Playwright, or just my stupidity? Thanks anyways for the help, I really appreciate it. People like you make a difference 😗

0reactions
aslushnikovcommented, Dec 6, 2022

But I don’t know how to say in the catch part to wait for my 2 guys (endpoint and navigation to the URL) since I already clicked on the checkout button before and I fear that waiting for those things in the catch block might be pointless since there is no triggering action with them (which is clicking the checkout button that happened already in the try block).

IIUC you can just extract those promises and use them in both parts of your program:

let promises = [];
try {
    await Promise.all([
      this.page.waitForNavigation({ url: '**/login' }),
      this.#checkoutBtn.click(),
    ]);
    const credentials = await Utils.getCredentials(this.#context);
    await this.#loginPage.email.fill(credentials.username);
    await this.#loginPage.password.fill(credentials.password);

    const promises = [
      this.page.waitForResponse(
        (response) =>
          response.url().includes('/my-account/addressform') &&
          response.status() === ResponseCode.SUCCESS
      ),
      this.page.waitForURL(/.*\/delivery-address\/add$/),
    ];
    await Promise.all([
      ...promises,
      this.#loginPage.loginBtn.click(),
    ]);
  } catch (error) {
    console.log('USER WAS NOT LOGGED OUT');
    await Promise.all(promises);
  }

So this would be my recommendation if I understood you correctly.

However, this is not a Playwright issue - more of a programming challenge. I’d recommend asking this elsewhere - StackOverflow, or maybe even https://aka.ms/playwright/slack. I’ll close this since this doesn’t require attention from the Playwright team specifically.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to use if expression in Promise.all()? - Stack Overflow
I want to use Promise.all() to handle two promise object, but the second is inner a if expression. How to handle this situation?...
Read more >
Promise.all() - JavaScript - MDN Web Docs
The Promise.all() method takes an iterable of promises as input and returns a single Promise . This returned promise fulfills when all of ......
Read more >
JavaScript | Promise.all() Method - GeeksforGeeks
It returns a single Promise that resolves when all of the promises passed as an iterable, which have resolved or when the iterable...
Read more >
Promises chaining - The Modern JavaScript Tutorial
A Promise keeps an internal state. It calls the handler wrapped in a try...catch block and stores the error. That is why asynchronous...
Read more >
JavaScript Interview Question | Implementing Promise.all ...
In this video, we're going to implement javascript Promise. all function ourselves. This is a question asked in many companies to see how ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found