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

request-model

v1.0.4

Published

A async task excute lib with the chain usage API.

Downloads

34

Readme

request-model

request-model is a useful async function management lib and help you coding more happy.

If you like to make your async function sequential execution and in a chain usage, you will like request-model.

Installation

npm install --save-dev request-model

Usage

import requestModel from 'request-model';

const rModel = new RequestModel({
    request: {
        getNameById(params) {
            return new Promise((resolve, reject) => {
                setTimeout(() => {
                    resolve(1);
                }, 2000);
            });
        },
        enums(params, params2) {
            return new Promise((resolve, reject) => {
                setTimeout(() => {
                    resolve(2);
                }, 1000);
            });
            // 返回一个Promise
        },
    },
});

// first getNameById and finsh start enums
rModel
    .chain()
    .commit('getNameById', 1)
    .commit('enums', 2);

API

Constructor(request-model)

import requestModel from 'request-model';

const rModel = new RequestModel({
    ...options,
});
Construction options
  • request:

    type { [type: string]: Function }

  • config: config for requestModel, the sub module config will extends its parent config.

    | key | type | default | description | | :---------- | :------ | :------ | :--------------------------------------- | | promiseWrap | boolean | false | Wrap your request fucntion with Promise. |

  • module: sub modules to request-model(multistage sub modules support now)

    type [type: string]: another request-model options like

    {
      key: {
        request: {
          [string]: Function
        },
        modules: {
            ...
        }
      }
    }
  • action: the collection of the commit.

    type [type: string]: Function

    params (chain: Chain, ...args: any[])

    action: {
        init(chain, ...args) {
            return chain
                .commit('getNameById', args[0])
                .commit('enums', args[1]);
        },
    }

Methods

  • chain

    Use to start the chain usage.

    return new Chain()

  • collection Use to get a collection.

    return new Collection()

  • PROMISEWRAP(static)

    Wrap a function to promise.

    params(fn: Function) returnFunction

  • commitWrap

    params key: string (the function name in the constructor request)

  • commitAll

    params The return Array of commitWrap

    etc:

    rModel
        .chain()
        .commit('getNameById', 1)
        .then(data => {
            return rModel.commitAll([
                rModel.commitWrap('enums2', 2),
                rModel.commitWrap('enums4', 4),
            ]);
        .finish(data => {
            // data = [1 [2, 4]]
        });
  • every method in Chain can be use and the same input and output.

    etc:

    rModel.chain().commit('getNameById', 1);
    // same as
    rModel.commit('getNameById', 1);

Chain

generator after requestModel.chain()

Methods

  • commit

    params key: string

    return Chain

  • then

    join your commit chain with another Function.

    params (resolve: Function, ?reject: Function, ?always: Function, ?before: Function)

    return Chain

  • finish / finally (same usage)

    add a finish Function will resolve after all the commit.

    params (resolve: Function, ?reject: Function, ?always: Function)

    return Chain

  • catch

    add a error catch Functin.

    params (reject: Function)

    return Chain

  • always

    add a always Functin. It will be called when after last resolve or reject(also resolve and reject could be empty) not support usage with always only(without commit/then)

    params (alwaysFn: Function)

    return Chain

Collection(extends Chain)

generator after requestModel.collection().

  • add add a Function into the collection.

    paramsadd(fn: Function, key?: string) The key is not nessary if the fn has its name(not anoymous) return Collection etc:

    const collection = rModel.collection();
    collection.add(
        RequestModel.PROMISEWRAP((resolve, reject, params) => {
            setTimeout(() => {
                resolve(params);
            }, 0);
        }),
        'a',
    );
    collection.add(
        RequestModel.PROMISEWRAP((resolve, reject, params) => {
            setTimeout(() => {
                resolve(params);
            }, 0);
        }),
        'b',
    );
    collection
        .commit('a', 1)
        .commit('b', 2)
        .finish(data => {
            assert.deepEqual(data, [1, 2]);
            done();
        });