Advertisement
Guest User

Untitled

a guest
Oct 27th, 2016
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.79 KB | None | 0 0
  1. export default class AsyncTask extends Promise {
  2.  
  3. constructor(executor) {
  4. let rejectFunc;
  5.  
  6. super((resolve, reject) => {
  7. rejectFunc = reject;
  8.  
  9. if(executor.constructor === Promise) {
  10. executor.then(resolve, reject);
  11. } else {
  12. executor(resolve, reject);
  13. }
  14. });
  15.  
  16. this.state = 'pending';
  17. this.cancel = rejectFunc.bind(null, {cancelled: true});
  18. }
  19.  
  20. then(onFulfilled, onRejected) {
  21. const fulfill = (result) => {
  22. this.state = 'fulfilled';
  23.  
  24. if(typeof onFulfilled === 'function') {
  25. return onFulfilled(result);
  26. }
  27. };
  28.  
  29. const reject = (error) => {
  30. this.state = 'rejected';
  31.  
  32. if(typeof onRejected === 'function') {
  33. return onRejected(error);
  34. }
  35. };
  36.  
  37. return super.then(fulfill, reject);
  38. }
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement