From b688dee4c8fb77cf34f80957d0d219ffcd5a7fc1 Mon Sep 17 00:00:00 2001 From: vsavkin Date: Fri, 26 Jun 2015 15:58:52 -0700 Subject: [PATCH] feat(async): added PromiseWrapper.wrap --- modules/angular2/src/facade/async.dart | 4 ++++ modules/angular2/src/facade/async.ts | 10 ++++++++++ 2 files changed, 14 insertions(+) diff --git a/modules/angular2/src/facade/async.dart b/modules/angular2/src/facade/async.dart index 1f1d79e556..a15fc66edd 100644 --- a/modules/angular2/src/facade/async.dart +++ b/modules/angular2/src/facade/async.dart @@ -19,6 +19,10 @@ class PromiseWrapper { return promise.then(success, onError: onError); } + static Future wrap(Function fn) { + return new Future(fn); + } + // Note: We can't rename this method to `catch`, as this is not a valid // method name in Dart. static Future catchError(Future promise, Function onError) { diff --git a/modules/angular2/src/facade/async.ts b/modules/angular2/src/facade/async.ts index f3f2e18743..c8a80a9d2c 100644 --- a/modules/angular2/src/facade/async.ts +++ b/modules/angular2/src/facade/async.ts @@ -28,6 +28,16 @@ export class PromiseWrapper { return promise.then(success, rejection); } + static wrap(computation: () => T): Promise { + return new Promise((res, rej) => { + try { + res(computation()); + } catch (e) { + rej(e); + } + }); + } + static completer() { var resolve; var reject;