Advertisement
Guest User

Untitled

a guest
May 28th, 2016
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.14 KB | None | 0 0
  1. interface PromiseStatePending {
  2. type: 'pending';
  3. }
  4.  
  5. interface PromiseStateFulfilled<T> {
  6. type: 'fulfilled';
  7. value: T;
  8. }
  9.  
  10. interface PromiseStateRejected {
  11. type: 'rejected';
  12. error: any;
  13. }
  14.  
  15. type PromiseState<T> = PromiseStatePending | PromiseStateFulfilled<T> | PromiseStateRejected;
  16.  
  17. namespace PromiseState {
  18.  
  19. export function fromPromise<T>(promise : PromiseLike<T>, callback: (state : PromiseState<T>) => void) {
  20. callback({ type: 'pending' });
  21. promise.then(value => callback({ type: 'fulfilled', value }), error => callback({ type: 'rejected', error }));
  22. }
  23.  
  24. export function isPending<T>(state : PromiseState<T>) : state is PromiseStatePending {
  25. return state.type === 'pending';
  26. }
  27.  
  28. export function isFulfilled<T>(state : PromiseState<T>) : state is PromiseStateFulfilled<T> {
  29. return state.type === 'fulfilled';
  30. }
  31.  
  32. export function isRejected<T>(state : PromiseState<T>) : state is PromiseStateRejected {
  33. return state.type === 'rejected';
  34. }
  35. }
  36.  
  37. PromiseState.fromPromise<number>(null, state => {
  38.  
  39. if(PromiseState.isPending(state)) {
  40.  
  41. } else if(PromiseState.isFulfilled(state)) {
  42.  
  43. } else if(PromiseState.isRejected(state)) {
  44.  
  45. }
  46.  
  47. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement