JavaScript waitForever

By  on  
Writing mochitests for new features in DevTools can be difficult and time-consuming.  There are so many elements interacting in an async manner that I oftentimes find myself using the debugger to debug the debugger!  In the case where it's unclear what interaction isn't working properly, I find myself going to a neat utility function: waitForever.  By using this function, paired with await, I can interact with the page while a given test is running to find the problem!

The JavaScript

waitForever is a super small snippet:
function waitForever() {
  return new Promise(r => {});
}

// Usage:
await waitForever();
The function uses a promise which never resolves, thus no additional statements are triggered while also not locking up the browser.  From there I can click around and explore elements to find what I've messed up in my test. Utility functions like these make web development much easier and more enjoyable.  Add this one to your toolbox!

Recent Features

  • By
    An Interview with Eric Meyer

    Your early CSS books were instrumental in pushing my love for front end technologies. What was it about CSS that you fell in love with and drove you to write about it? At first blush, it was the simplicity of it as compared to the table-and-spacer...

  • By
    Facebook Open Graph META Tags

    It's no secret that Facebook has become a major traffic driver for all types of websites.  Nowadays even large corporations steer consumers toward their Facebook pages instead of the corporate websites directly.  And of course there are Facebook "Like" and "Recommend" widgets on every website.  One...

Incredible Demos

  • By
    Creating the Treehouse Frog Animation

    Before we start, I want to say thank you to David for giving me this awesome opportunity to share this experience with you guys and say that I'm really flattered. I think that CSS animations are really great. When I first learned how CSS...

  • By
    CSS calc

    CSS is a complete conundrum; we all appreciate CSS because of its simplicity but always yearn for the language to do just a bit more. CSS has evolved to accommodate placeholders, animations, and even click events. One problem we always thought...

Discussion

  1. Kenji Okamoto

    Thanks for the tip David! I’ll keep that in mind when working on mochi tests. Have you found any other uses for this snippet?

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!