Advertisement
Guest User

Untitled

a guest
May 21st, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.     flatMap<S>(f: (T) => Future<S>): Future<S> {
  2.         let p = new CompletablePromise<S>();
  3.         let fut = p.future;
  4.         this.next = (v) => {
  5.             if (v.isSuccess()) {
  6.                 let result: Try<Future<S>> = Try.doWork(() => f(v.get()));
  7.                 if (result.isSuccess()) {
  8.                     let nextFut = result.get();
  9.                     nextFut.onComplete((v) => {
  10.                         p.complete(v)
  11.                     })
  12.                 } else {
  13.                     p.complete(result as Try<any> as Try<S>)
  14.                 }
  15.             } else {
  16.                 p.complete(v as Try<any> as Try<S>);
  17.             }
  18.         };
  19.  
  20.         return fut;
  21.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement