npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@agabhane/puppeteer-pending-requests

v1.0.1

Published

Wait for pending requests to finish in puppeteer

Downloads

31

Readme

puppeteer-pending-requests

Introduction

Provides utility to wait for network to be idle for passed time.

This utility basically checks for zero pending requests and there is no new request for passed time.

Install

npm install @agabhane/puppeteer-pending-requests

Usage

Import @agabhane/puppeteer-pending-requests in your code

const { PuppeteerPendingRequests } = require('@agabhane/puppeteer-pending-requests');

Create an instance by passing Puppeteer page

let puppeteerPendingRequests = new PuppeteerPendingRequests(page);

Use waitForNetworkIdle() function to wait for network to be idle for passed time.

await waitForNetworkIdle(500);

waitForNetworkIdle(500) function returns promise which gets resolved when network was idle for passed time (in this example 500 ms)

To avoid race condition, please use below syntax.

await Promise.all([       
    puppeteerPendingRequests.waitForNetworkIdle(500),
    await this.someElement.click()
]);

Example

Example 1

Lets say there are two xhr requests happen when we click on a button, 1st request happens as soon as we click on button and 2nd request happens after 200 milliseconds after receiving 1st requests response.

1req------|
           ----200ms----|
                        2req--------|
                                     ----250ms (network idle)-----
                                

We want our function to wait for both requests to finish.

We will call our function with time more than 200 (lets say 250) so that we will wait for both requests to finish

await Promise.all([       
    puppeteerPendingRequests.waitForNetworkIdle(250),
    await this.button.click()
]);

Example 2

Lets say there are three xhr requests happen simultaneously when we click on a button.

1req------|
2req---------------|
3req---|
                    ------100 ms (network idle) -----

We want our function to wait for all requests to finish.

We will call our function with time more than 0 (lets say 100 default). Since we all requests happening at the same time, time does not matter. Function will wait untill all requests finish.

await Promise.all([       
    puppeteerPendingRequests.waitForNetworkIdle(100),
    await this.button.click()
]);

Documentation

PuppeteerPendingRequests

Constructor function takes puppeterr page as paramter and returns instance

Params | Name | Type | Default | Description | | --- | --- | --- | --- | | page | Puppeteer Page Object | | |

Returns

PuppeteerPendingRequests instance

waitForNetworkIdle

Wait for network to be idle for said time

Params | Name | Type | Default | Description | | --- | --- | --- | --- | | time | number | 100 | This will make function to wait for network to be idle for 100ms | | resourceType | string | 'xhr' | Resource type to wait for

Returns

Promise which will get resolved when there is network idle for passed time for specific resource type