Guest User

Untitled

a guest
Jan 17th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.00 KB | None | 0 0
  1. export class BusyPromise {
  2.  
  3. /**
  4. * Promise wrapper that calls pre and post methods. Can be used to auto increment and decrement a
  5. * counter, for example, or to indicate when busy and unbusy.
  6. * @param opts {Object}
  7. * @param [opts.pre] {Function} Function to execute before calling function
  8. * @param [opts.onResolve] {Function} Function to execute after function resolves
  9. * @param [opts.onReject] {Function} Function to execute if function rejects
  10. * @param fn {function} Same as function in new Promise(fn)
  11. */
  12. constructor (opts, fn) {
  13. this.opts = opts;
  14. this.opts.pre && this.opts.pre();
  15. this.p = new Promise((resolve, reject) => {
  16. fn((data) => {
  17. this.opts.onResolve && this.opts.onResolve();
  18. resolve(data);
  19. }, (err) => {
  20. this.opts.onReject && this.opts.onReject(err);
  21. reject(err);
  22. });
  23. });
  24. }
  25.  
  26. then (onResolve, onReject) {
  27. return this.p.then(onResolve, onReject);
  28. }
  29.  
  30. catch (onReject) {
  31. return this.p.catch(onReject);
  32. }
  33. }
Add Comment
Please, Sign In to add comment