New Self New Life
No Result
View All Result
  • Home
  • Entertainment
  • Celebrity
  • Cinema
  • Music
  • Digital Lifestyle
  • Social Media
  • Softwares
  • Devices
  • Home
  • Entertainment
  • Celebrity
  • Cinema
  • Music
  • Digital Lifestyle
  • Social Media
  • Softwares
  • Devices
New Self New Life
No Result
View All Result
Home Softwares

JavaScript waitFor Polling

by admin
2 years ago
in Softwares
Object.entries
Share on FacebookShare on Twitter

As extra of the JavaScript builders write turns into asynchronous, it is solely pure to want to attend for circumstances to be met. That is very true in a world with asynchronous testing of circumstances which do not present an express await. I’ve written about waitForever, waitForTime, and JavaScript Polling previously, however I needed to have a extra trendy approach of awaiting a given state. Let’s take a look at this tremendous helpful waitFor operate!

waitFor is an async operate that enables builders to offer a situation operate, polling interval (in milliseconds), and optionally available timeout (in milliseconds).

// Polls each 50 milliseconds for a given situation
const waitFor = async (situation, pollInterval = 50, timeoutAfter) => {
  // Observe the beginning time for timeout functions
  const startTime = Date.now();

  whereas (true) {
    // Verify for timeout, bail if an excessive amount of time handed
    if(typeof(timeoutAfter) === 'quantity' && Date.now() > startTime + timeoutAfter) {
      throw 'Situation not met earlier than timeout';
    }

    // Verify for conditon instantly
    const outcome = await situation();

    // If the situation is met...
    if(outcome) {
      // Return the outcome....
      return outcome;
    }

    // In any other case wait and verify after pollInterval
    await new Promise(r => setTimeout(r, pollInterval));
  }
};

Utilizing this operate is so simple as simply offering a situation operate:

await waitFor(() => doc.physique.classList.has('loaded'));

Timing out the interval and timeout can also be easy:

await waitFor(
  () => doc.physique.classList.has('loaded'),
  // Checks each 100 milliseconds
  100,
  // Throws if the "loaded" class is not on the physique after 1 second
  10000
);

In a super world, builders would all the time have a deal with on the Promise that may very well be await‘d or then‘d. In follow, nevertheless, that is not all the time the case, particularly in a testing atmosphere. Having the ability to await a situation in any atmosphere is an absolute should, so maintain this snippet in your toolbox!

Website performance monitoring
Website performance monitoring


Source link
Tags: JavaScriptPollingwaitFor
Previous Post

8 Moisture-Wicking Pieces That Will Help Keep You Cool

Next Post

The Big Summer Sweater Roundup

Related Posts

10+ Best Free Portfolio & Lookbook Templates for InDesign in 2025 — Speckyboy
Softwares

10+ Best Free Portfolio & Lookbook Templates for InDesign in 2025 — Speckyboy

by admin
June 20, 2025
User Guide For CS-Cart Product Search By Barcode
Softwares

User Guide For CS-Cart Product Search By Barcode

by admin
June 18, 2025
Open Talent platforms emerging to match skilled workers to needs, study finds
Softwares

Open Talent platforms emerging to match skilled workers to needs, study finds

by admin
June 16, 2025
New tool could help homeowners weather flood risks, lower insurance costs
Softwares

New tool could help homeowners weather flood risks, lower insurance costs

by admin
June 19, 2025
pros, cons, and How to Use Flutter
Softwares

pros, cons, and How to Use Flutter

by admin
June 13, 2025
Next Post
The Big Summer Sweater Roundup

The Big Summer Sweater Roundup

Mark Zuckerberg says Elon Musk ‘isn’t serious’ about cage fight – National

Mark Zuckerberg says Elon Musk ‘isn’t serious’ about cage fight - National

  • Trending
  • Comments
  • Latest
8BitDo Retro Mechanical Keyboard C64 Review

8BitDo Retro Mechanical Keyboard C64 Review

March 24, 2025
SOG and Leatherman EDC, Dyson Lightcycle Morph lamp, COTRE 2-way radios, and more – Weekly roundup

SOG and Leatherman EDC, Dyson Lightcycle Morph lamp, COTRE 2-way radios, and more – Weekly roundup

May 16, 2021
Guide for Bagisto Quick Commerce

Guide for Bagisto Quick Commerce

October 16, 2024
The Best Madras Shirt Brands For Men: Summer 2021 Edition

The Best Madras Shirt Brands For Men: Summer 2021 Edition

July 20, 2021
HTML and CSS for Beginners [Article + Tutorial]

HTML and CSS for Beginners [Article + Tutorial]

April 28, 2021
The Definitive 30-Step Basic SEO Checklist for 2022

The Definitive 30-Step Basic SEO Checklist for 2022

January 3, 2022
How to Build a JavaScript Search [Article]

How to Build a JavaScript Search [Article]

August 30, 2022
Best Coding Practices For Rest API Design

Django Form | Data Types and Fields

June 9, 2023
Jim Jones Rejects Notion That His Career Is Comparable to Nas’

Jim Jones Rejects Notion That His Career Is Comparable to Nas’

June 20, 2025
Latest stock updates at Best Buy, Walmart, Target and more

Latest stock updates at Best Buy, Walmart, Target and more

June 20, 2025
Gilmore Girls Took The Show’s Most Controversial Storyline A Little Too Far

Gilmore Girls Took The Show’s Most Controversial Storyline A Little Too Far

June 20, 2025
Singapore Q3 job outlook: Who’s hiring? Opportunities, risks & forecasts amid Trump tariffs.

Singapore Q3 job outlook: Who’s hiring? Opportunities, risks & forecasts amid Trump tariffs.

June 20, 2025
The Largest Communities on Reddit [Infographic]

The Largest Communities on Reddit [Infographic]

June 20, 2025
Rebel Wilson Says ‘It’s Really Hard Being a Working Mum,’ Spills on Year One of Marriage

Rebel Wilson Says ‘It’s Really Hard Being a Working Mum,’ Spills on Year One of Marriage

June 20, 2025
Justin Bieber Trolls Haters Over 'Standing On Business' Viral Paparazzi Outburst!

Justin Bieber Trolls Haters Over 'Standing On Business' Viral Paparazzi Outburst!

June 20, 2025
Robin Tunney, Ryan Simpkins & Aaron Dominguez Join Emily Robinson’s ‘Ugly Cry’

Robin Tunney, Ryan Simpkins & Aaron Dominguez Join Emily Robinson’s ‘Ugly Cry’

June 19, 2025
New Self New Life

Your source for entertainment news, celebrities, celebrity news, and Music, Cinema, Digital Lifestyle and Social Media and More !

Categories

  • Celebrity
  • Cinema
  • Devices
  • Digital Lifestyle
  • Entertainment
  • Music
  • Social Media
  • Softwares
  • Uncategorized

Recent Posts

  • Jim Jones Rejects Notion That His Career Is Comparable to Nas’
  • Latest stock updates at Best Buy, Walmart, Target and more
  • Gilmore Girls Took The Show’s Most Controversial Storyline A Little Too Far
  • Home
  • Disclaimer
  • DMCA
  • Privacy Policy
  • Cookie Privacy Policy
  • Terms and Conditions
  • Contact us

Copyright © 2021 New Self New Life.
New Self New Life is not responsible for the content of external sites. slotsfree  creator solana token

No Result
View All Result
  • Home
  • Entertainment
  • Celebrity
  • Cinema
  • Music
  • Digital Lifestyle
  • Social Media
  • Softwares
  • Devices

Copyright © 2021 New Self New Life.
New Self New Life is not responsible for the content of external sites.

New Self New Life