Coder Social home page Coder Social logo

seq's Introduction

seq

这是一个简单的方法链式调用库

安装

npm i seq-fns

yarn add seq-fns

使用

const seq = require('seq')

seq([
  x => x + 1,
  async x => x + 2,
  x => new Promise(resolve => setTimeout(() => resolve(x + 3), 500)),
])
  .run(1)
  .then(result => console.log(result))

// 7 (1 + 1 + 2 + 3)

你可以通过 add 方法动态增加处理函数

seq.add(x => x + 1)

seq 会根据传入函数的顺序依次执行,并且把上一次处理的结果作为下一个函数的参数传入,函数可以是同步的也可以是异步的,最终 run 方法都会返回的一个 promise 并返回调用结果

错误处理

seq([
  x => x + 1,
  x => {
    throw x
  },
])
  .run(1)
  .catch(err => console.log(err))

// 2

任何在执行过程中的报错都可以在返回的promise中被捕获,包括:

  • 同步 throw 的错误
  • promise reject 的错误
  • 异步方法 throw 的错误

roadmap

  1. 增加多参数的场景
  2. 增加回调形式的异步函数

seq's People

Contributors

jokcy avatar

Stargazers

 avatar

Watchers

 avatar  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.