daily pastebin goal
1%
SHARE
TWEET

Untitled

a guest May 16th, 2018 118 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. export class CustomPromise {
  2.  
  3.   constructor (executor) {
  4.     if (typeof executor !== 'function'){
  5.       throw new Error('You must pass a function')
  6.     }
  7.     // Etat de notre promesse
  8.     let _state = State.PENDING;
  9.     this.setState = setter(_state)
  10.     this.getState = getter(_state)
  11.  
  12.     // Value représente la valeur actuelle de la promesse une fois résolue
  13.     let _value = null
  14.     this.setValue = setter(_value)
  15.     this.getValue =  getter(_value)
  16.  
  17.     // Chained concentre l'ensemble des actions a executions à la resolution de la promesse
  18.     let _chained = []
  19.     this.setChained = setter(_chained)
  20.     this.getChained = getter(_chained)
  21.  
  22.     try {
  23.       executor(resolve, reject);
  24.     } catch (err){
  25.       this.reject(err)
  26.     }
  27.   }
  28.  
  29.   resolve = res => {
  30.     if (this.getState() !== State.PENDING) {
  31.       return
  32.     }
  33.     const then = res != null ? res.then : null;
  34.     if (typeof then === 'function') {
  35.       return then(resolve, reject);
  36.     }
  37.  
  38.     this.setState(State.FULFILLED);
  39.     this.setValue(res);
  40.  
  41.     this.getChained().forEach(({onFulfilled, onRejected}) => { onFulfilled(res) });
  42.  
  43.     return res;
  44.   }
  45.  
  46.   reject = reason => {
  47.     if (this.getState() !== State.PENDING) {
  48.       return
  49.     }
  50.  
  51.     this.setState(State.REJECTED);
  52.     this.setValue(reason);
  53.   }
  54.  
  55.   then(onFullFiled, onRejected){
  56.     // On retourne une promesse pour pouvoir chainer les .then
  57.     return new CustomPromise((resolve, reject) => {
  58.  
  59.       const _onFulfilled = res => {
  60.         try {
  61.           resolve(onFulfilled(res));
  62.         } catch (err) {
  63.           reject(err);
  64.         }
  65.       };
  66.       const _onRejected = err => {
  67.         try {
  68.           reject(onRejected(err));
  69.         } catch (_err) {
  70.           reject(_err);
  71.         }
  72.       };
  73.  
  74.  
  75.       switch(this.getState()) {
  76.         case State.FULFILLED:
  77.           _onFullFiled(this.getValue());
  78.           break;
  79.         case State.REJECTED:
  80.           _onRejected(this.getValue());
  81.           break;
  82.         default:
  83.           this.setChained(this.getChained().push({ onFulfilled: _onFulfilled, onRejected: _onRejected }))
  84.       }
  85.     })
  86.    
  87.  
  88.   }
  89.  
  90. }
  91.  
  92. const State = Object.freeze({
  93.   REJECTED: Symbol('REJECTED'),
  94.   PENDING: Symbol('PENDING'),
  95.   FULFILLED: Symbol('FULFILLED')
  96. });
  97.  
  98. const setter = key => val => { key = val}
  99. const getter = key => () => key
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top