Socket
Book a DemoInstallSign in
Socket

request-state-wrapper

Package Overview
Dependencies
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

request-state-wrapper

A tiny package for managing the state of asynchronous requests.

Source
npmnpm
Version
0.0.2
Version published
Maintainers
1
Created
Source

Request State Wrapper

A simple wrapper for Asynchronous JavaScript requests that allows you to detect stalled, fetching and finished states.

What is the point of this tool? Read Better loading states for the modern web.

API

import { createRequest } from 'request-state-wrapper';

// Create your smart request
const request = createRequest({
    request: [<Promise>], // Array of Promises
    stalledDelay: Number, // Time in MS before we consider a request stalled
    onStateChange: Function, // Callback executed every time request state changes
});

// run it!
request();

Example

// Before

// Set our loading state
loading = true

// Start our asyncronous request/s
Promise.all([asyncRequest(), anotherAsyncRequest()]).then(payload => {

    // When it's finished, set loading state to false
    loading = false

    // Then handle the response
    if(payload.error) // show error screen
    // show success screen
});

// After
import { createRequest } from 'request-state-wrapper';

const getData = createRequest({
    request: [asyncRequest, anotherAsyncRequest],
    stalledDelay: 250,
    onStateChange: state => { ... },
})

getData().then(payload => console.log(payload));

With async/await

// Before
async function requests () {
    const payload = await Promise.all[asyncRequest(), anotherAsyncRequest()];
    console.log(payload);
}

// After
import { createRequest } from 'request-state-wrapper';

const getData = createRequest({
    request: [asyncRequest, anotherAsyncRequest],
    stalledDelay: 250,
    onStateChange: state => { ... },
})

const data = await getData();

Add a specific handler for each type of event:

import { createRequest } from 'request-state-wrapper';

const getData = createRequest({
    request: [asyncRequest(), anotherAsyncRequest()],
    stalledDelay: 250,
    onFetching: state => { ... },
    onStalled: state => { ... },
    onFinished: state => { ... },
})

Note, specific handler will override onStateChange().

Create your request, and add/override handlers when you run the request:

import { createRequest } from 'request-state-wrapper';

const getData = createRequest({
    request: [asyncRequest(), anotherAsyncRequest()],
    stalledDelay: 250,
})

const data = await getData({ onStateChange: state => { ... } });

Want some more examples? Check out some demo recipes:

Simple UI with React Simple UI with React Hooks NodeJS server

FAQs

Package last updated on 27 Mar 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts