Giter Site home page Giter Site logo

promise-chain's Introduction

promise-chain

创建1个 promise 执行链,可手动暂停、继续、取消、结束、重启

安装

npm

npm i @liuyunjs/promise-chain --S

yarn

yarn add @liuyunjs/promise-chain

Api

chain: () => Operators;

创建一个执行链, 返回一组操作符

Operators

use: (onFulfilled?: (valueOrReason: any, operators: Operators) => any, onRejected?: (valueOrReason: any, operators: Operators) => any) => number;

向执行链中添加一组任务,返回本组任务的下标;可使用 eject 取消任务

eject: (index: number) => void

从执行链中取消对应任务

run: (params?: any) => any;

启动执行链,已经被启动的执行链如果需要重启应该调用 rerun 方法

pause: (value?: any) => void;

暂停正在执行中的执行链,可传入一个参数替代目前执行链的结果

restart: (value?: any)=>void;

重新启动一个暂停中的执行链,可传入一个参数替代目前执行链的结果

stop: (value?: any) => void;

停止正在执行中的执行链,调用后 run 函数会变为 fulfilled 可传入一个参数替代执行链的结果, 就是 run 函数最终执行完成的返回值

rerun: (params?: any) => void;

重新启动执行链, 可传入一个参数替代执行 run 时传入的参数

cancel: (reason?: string) => void;

取消执行中的执行链,调用后 run 函数会变为 rejected 可传入一个字符串作为 run 函数最终失败的错误提醒

示例

import { chain } from '@liuyunjs/promise-chain';

const operators = chain();
operators.use(
  (params,operators) => {
    console.log(params);
    
    return new Promise((resolve, reject) => {
      setTimeout(() => {

          // operators.restart('restart');
          // operators.pause('pause');
          // operators.rerun('rerun');
          // operators.stop('stop');
          // operators.cancel('cancel');

          // reject(new Error('reject'));
        resolve('setTimeout');
      }, 3000);
    });
  },
  (reason,operators) => {
    console.log('err', reason);
    return Promise.reject(reason);
  },
);

operators.run('chain run').then(console.log, console.log);

promise-chain's People

Contributors

liuyunjs avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.