Skip to content

unixzii/p-seq

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

p-seq Build Status Coverage Status NPM Version Dependencies Status

Run async tasks sequently

Install

$ npm install --save p-seq

Usage

const pSeq = require('p-seq');

// op1, op2, op3 is functions that return a Promise.
pSeq(op1, op2, op3)
  .then(result => {
    // Get the result of last task here.
  })
  .catch(err => {
    // Reach here if any error thrown.
  });

When the Promise of a task resolved, p-seq passes its result to next task generator. So there will be only one task is running at one time.

API

pSeq(tasks)

Arguments

  • tasks: An array of task generators, element type is Promise, Function or GeneratorFunction.

Return Value

A Promise represented the last task.

Discussion

When a task generator is Promise, p-seq just pass its result to next task. When it is a function or generator function, p-seq call it when last task is resolved, and pass the result of last task to it, the function should return a Promise, otherwise p-seq just pass the return value through. Since the generator is run via co, it produces a Promise intrinsically.

If a task generator's type is none of above, p-seq just pass itself through the chain, but we strongly recommend you to avoid doing that.

There is also a more convenient way to pass tasks:

pSeq(task1, task2, ...)

License

MIT © Cyandev

About

Run async tasks sequently

Resources

License

Stars

Watchers

Forks

Packages

No packages published