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

Verification framework uncovers safety lapses in open-source self-driving system
Softwares

Verification framework uncovers safety lapses in open-source self-driving system

by admin
May 23, 2025
PrestaShop Free Gift Products | Add Free Products to Cart
Softwares

PrestaShop Free Gift Products | Add Free Products to Cart

by admin
May 22, 2025
The emperors of AI coding tools have no clothes – and it’s creating a productivity delusion
Softwares

The emperors of AI coding tools have no clothes – and it’s creating a productivity delusion

by admin
May 20, 2025
Land Your First Programming Job in 7 Steps
Softwares

Land Your First Programming Job in 7 Steps

by admin
May 21, 2025
Blockchain gaming is ‘growing up’
Softwares

Blockchain gaming is ‘growing up’

by admin
May 19, 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
barnacle boi Releases Thumping ‘Introspect’ EP

barnacle boi Releases Thumping ‘Introspect’ EP

November 15, 2023
Australian Music Festival Forced to Cancel Due to 529% Government-Imposed Price Hike: Report

Australian Music Festival Forced to Cancel Due to 529% Government-Imposed Price Hike: Report

May 9, 2024
JetBrains Space Review | Developer.com JetBrains Space IDE Review

JetBrains Space Review | Developer.com JetBrains Space IDE Review

July 19, 2023
Most Useful Gadgets in 2021 – Nogentech.org

Most Useful Gadgets in 2021 – Nogentech.org

July 29, 2021
I Tried Calocurb For 90 Days. Here’s My Review.

I Tried Calocurb For 90 Days. Here’s My Review.

January 8, 2025
Cameron Monaghan Discusses Erotic Thriller

Cameron Monaghan Discusses Erotic Thriller

January 13, 2022
New Survey Looks at How Marketers are Approaching the Metaverse, Crypto and NFTs [Infographic]

New Survey Looks at How Marketers are Approaching the Metaverse, Crypto and NFTs [Infographic]

April 2, 2022
How to Build a JavaScript Search [Article]

How to Build a JavaScript Search [Article]

August 30, 2022
Which Celebrities Attended Diddy’s White Parties? – Hollywood Life

Which Celebrities Attended Diddy’s White Parties? – Hollywood Life

May 24, 2025
Verification framework uncovers safety lapses in open-source self-driving system

Verification framework uncovers safety lapses in open-source self-driving system

May 23, 2025
Fans React – First I Prevail Song Since Brian Burkheiser Split

Fans React – First I Prevail Song Since Brian Burkheiser Split

May 23, 2025
A two-pack of Blink Mini 2 security cameras is only $38 for Memorial Day

A two-pack of Blink Mini 2 security cameras is only $38 for Memorial Day

May 23, 2025
Guy Ritchie’s new movie called ‘one of the worst films of the year’ after huge streaming hits

Guy Ritchie’s new movie called ‘one of the worst films of the year’ after huge streaming hits

May 23, 2025
Bybit’s Shunyet on Bitcoin’s ATH: ‘$125K Is Within Reach’ in Q2

Bybit’s Shunyet on Bitcoin’s ATH: ‘$125K Is Within Reach’ in Q2

May 23, 2025
DOOMSDAY and SECRET WARS to December 2026 and 2027 — GeekTyrant

DOOMSDAY and SECRET WARS to December 2026 and 2027 — GeekTyrant

May 23, 2025
Instagram Adds New DM Options, Including Voice Clip Transcription

Instagram Adds New DM Options, Including Voice Clip Transcription

May 23, 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

  • Which Celebrities Attended Diddy’s White Parties? – Hollywood Life
  • Verification framework uncovers safety lapses in open-source self-driving system
  • Fans React – First I Prevail Song Since Brian Burkheiser Split
  • 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.

slot 777 games download free