feat(async): added PromiseWrapper.wrap
This commit is contained in:
parent
71e0f89594
commit
b688dee4c8
|
@ -19,6 +19,10 @@ class PromiseWrapper {
|
||||||
return promise.then(success, onError: onError);
|
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
|
// Note: We can't rename this method to `catch`, as this is not a valid
|
||||||
// method name in Dart.
|
// method name in Dart.
|
||||||
static Future catchError(Future promise, Function onError) {
|
static Future catchError(Future promise, Function onError) {
|
||||||
|
|
|
@ -28,6 +28,16 @@ export class PromiseWrapper {
|
||||||
return promise.then(success, rejection);
|
return promise.then(success, rejection);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static wrap<T>(computation: () => T): Promise<T> {
|
||||||
|
return new Promise((res, rej) => {
|
||||||
|
try {
|
||||||
|
res(computation());
|
||||||
|
} catch (e) {
|
||||||
|
rej(e);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
static completer() {
|
static completer() {
|
||||||
var resolve;
|
var resolve;
|
||||||
var reject;
|
var reject;
|
||||||
|
|
Loading…
Reference in New Issue