Simple Promise library that follows Promises/A+ specification.
You can install el-promise using npm.
$ npm install --save el-promise
For browser you have to use a module bundler like Webpack or Browserify or you can insert a script tag in you html like this:
<script src="/node_modules/el-promise/build/promise.min.js"></script>
that defines global Promise variable.
In NodeJS you can just require
it:
const Promise = require('el-promise');
The library follows the specs 100% except that there is also a Promise#finally method:
spinner.show();
fetchData()
.then((data) => {
// do something with data
})
.catch((err) => {
// handle error somehow
})
.finally(() => {
// here you have no arguments
spinner.hide();
// the value or the error from the previous promises stands
});
The method takes a function as an argument that takes 0 arguments. If the function throws an error or returns a rejected promise the returned promise will be rejected with the error. Otherwise the function returns a promise resolved with the previous resolved value or rejected with the previous rejected error. Also if the function returns a promise it will be first resolved but the resulting promise will be resolved with the previous value.
And there is also an abstract Promise#abort method (that may be used by other libraries or you).
For more convenience there are two functions (Promise.onError and Promise.onUnhandledRejection) that are called on any rejection in promise and on an unhandled rejection respectively (with the error argument). If the property (Promise.onError or Promise.onUnhandledRejection) is not a function nothing happens.