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
    Write Better JavaScript with Promises

    You've probably heard the talk around the water cooler about how promises are the future. All of the cool kids are using them, but you don't see what makes them so special. Can't you just use a callback? What's the big deal? In this article, we'll...

  • By
    CSS @supports

    Feature detection via JavaScript is a client side best practice and for all the right reasons, but unfortunately that same functionality hasn't been available within CSS.  What we end up doing is repeating the same properties multiple times with each browser prefix.  Yuck.  Another thing we...

Incredible Demos

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!