Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- export class BusyPromise {
- /**
- * Promise wrapper that calls pre and post methods. Can be used to auto increment and decrement a
- * counter, for example, or to indicate when busy and unbusy.
- * @param opts {Object}
- * @param [opts.pre] {Function} Function to execute before calling function
- * @param [opts.onResolve] {Function} Function to execute after function resolves
- * @param [opts.onReject] {Function} Function to execute if function rejects
- * @param fn {function} Same as function in new Promise(fn)
- */
- constructor (opts, fn) {
- this.opts = opts;
- this.opts.pre && this.opts.pre();
- this.p = new Promise((resolve, reject) => {
- fn((data) => {
- this.opts.onResolve && this.opts.onResolve();
- resolve(data);
- }, (err) => {
- this.opts.onReject && this.opts.onReject(err);
- reject(err);
- });
- });
- }
- then (onResolve, onReject) {
- return this.p.then(onResolve, onReject);
- }
- catch (onReject) {
- return this.p.catch(onReject);
- }
- }
Add Comment
Please, Sign In to add comment