promise-chaining
Yet another wrapper library for Promise
chaining.
Why chaining?
There are many Promise
implementations.
Not only that, you might use Deferred
preferably.
And there's a possibility that one of libraries you use returns custom-made thenable object.
Now you need to chain them.
Of course, chained functions/objects should wait for previous ones to finish execution.
Some thenable implementations might not handle thenable objects passed to resolve()
function.
This Chain
library supports chaining of those 'immature' Promise
implementations.
var promise1 = { } promise1
Example
Chain
1: Using var chain = // -> Task 1: start if SOME_CONDITION /* true, for example */ chain // Same with: chain.then(promiseGenerator.bind(null, 2)) chain if OTHER_CONDITION /* true, for example */ chain // Same with: chain.then(promiseGenerator.bind(null, 5)) chain// -> Task 1: done// -> Task 2: start// -> Task 2: done// -> Task 3: start// -> Task 3: done// -> Task 4: start// -> Task 4: done// -> Task 5: start// -> Task 5: done// -> All done
Promise
2: Equivalent code using ES2015 var chain = // -> Task 1: start if SOME_CONDITION /* true, for example */ chain = chain chain = chain if OTHER_CONDITION /* true, for example */ chain = chain chain// -> Task 1: done// -> Task 2: start// -> Task 2: done// -> Task 3: start// -> Task 3: done// -> Task 4: start// -> Task 4: done// -> Task 5: start// -> Task 5: done// -> All done
Predefined variables/functions for example codes above
// Example definitionvar SOME_CONDITION = truevar OTHER_CONDITION = true { // Creates and returns promise object return { console // Do an asynchronous task here... }} { // Creates and returns thenable object var resolve console // Do another asynchronous task here... return { resolve = callback } } { }
Installation
$ npm install promise-chaining --save
APIs
Please read test/promise-chaining.spec.js
for available APIs and example usages.
Usage
AMD
Browser globals
Load script first:
yields global class function Chain
.
With Chain
class:
var chain =
Node.js
const Chain = let chain =
Issues
Feel free to submit issues and enhancement requests.
Contributing
Please refer to JavaScript Standard Style and follow the guideline for submitting patches and additions.
I slightly modified the rule for this project... no space after function name because Sublime Text fails to parse function names if there's space after function name:
{ ... }
See .eslintrc for detail.
License
promise-chaining
is made available under the terms of the MIT license.