Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- export default class AsyncTask extends Promise {
- constructor(executor) {
- let rejectFunc;
- super((resolve, reject) => {
- rejectFunc = reject;
- if(executor.constructor === Promise) {
- executor.then(resolve, reject);
- } else {
- executor(resolve, reject);
- }
- });
- this.state = 'pending';
- this.cancel = rejectFunc.bind(null, {cancelled: true});
- }
- then(onFulfilled, onRejected) {
- const fulfill = (result) => {
- this.state = 'fulfilled';
- if(typeof onFulfilled === 'function') {
- return onFulfilled(result);
- }
- };
- const reject = (error) => {
- this.state = 'rejected';
- if(typeof onRejected === 'function') {
- return onRejected(error);
- }
- };
- return super.then(fulfill, reject);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement