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
    LightFace:  Facebook Lightbox for MooTools

    One of the web components I've always loved has been Facebook's modal dialog.  This "lightbox" isn't like others:  no dark overlay, no obnoxious animating to size, and it doesn't try to do "too much."  With Facebook's dialog in mind, I've created LightFace:  a Facebook lightbox...

  • By
    Designing for Simplicity

    Before we get started, it's worth me spending a brief moment introducing myself to you. My name is Mark (or @integralist if Twitter happens to be your communication tool of choice) and I currently work for BBC News in London England as a principal engineer/tech...

Incredible Demos

  • By
    Create Digg URLs Using PHP

    Digg recently came out with a sweet new feature that allows users to create Tiny Digg URLs which show a Digg banner at the top allowing easy access to vote for the article from the page. While I love visiting Digg every once in a...

  • By
    Fading Links Using jQuery:  dwFadingLinks

    UPDATE: The jQuery website was down today which caused some issues with my example. I've made everything local and now the example works. Earlier this week, I posted a MooTools script that faded links to and from a color during the mouseover and mouseout events.

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!