An extended promise to keep the context persistently
npm install with-promise
In browser:
<script src="dist/with-promise.js"></script>
In an AMD loader:
require('with-promise', function (WithPromise) {/*....*/});
In nodejs:
var WithPromise = require('with-promise');
- Extended Promise, make all
.then()
,.catch()
handlers be executed with your assigned context:this
.
You should use polyfills providing Promise to ensure with-promise works well. You can try polyfill.io or polyfills.io. For nodejs, you can try es6-promise or ypromise.
var WithPromise = require('with-promise');
// create a Promise by a resolver function and set context
var myPromise = WithPromise.create(resolver, context);
// create a resolved Promise
var myPromise = WithPromise.resolve(value, context);
// create a rejected Promise
var myPromise = WithPromise.reject(value, context);
// wrap Promise.all with context
var allPromises = WithPromise.all(promises, context);
// all this == context
myPromise.then(function () {
// this == context
}).then(function () {
// this == context
}).catch(function () {
// this == context
});