Index: src/js/harmony-async-await.js |
diff --git a/src/js/harmony-async-await.js b/src/js/harmony-async-await.js |
index c6705efe36d027134dc4cbf09003d27e107fe0d4..d9fbba307a55f61e7e2642774bf2ef2fc6f93a28 100644 |
--- a/src/js/harmony-async-await.js |
+++ b/src/js/harmony-async-await.js |
@@ -13,29 +13,44 @@ |
var AsyncFunctionNext; |
var AsyncFunctionThrow; |
-var PromiseReject; |
-var PromiseResolve; |
-var PromiseThen; |
+var IsPromise; |
+var GlobalPromise; |
+var NewPromiseCapability; |
+var PerformPromiseThen; |
utils.Import(function(from) { |
AsyncFunctionNext = from.AsyncFunctionNext; |
AsyncFunctionThrow = from.AsyncFunctionThrow; |
- PromiseReject = from.PromiseCreateRejected; |
- PromiseResolve = from.PromiseCreateResolved; |
- PromiseThen = from.PromiseThen; |
+ IsPromise = from.IsPromise; |
+ GlobalPromise = from.GlobalPromise; |
+ NewPromiseCapability = from.NewPromiseCapability; |
+ PerformPromiseThen = from.PerformPromiseThen; |
}); |
// ------------------------------------------------------------------- |
function AsyncFunctionAwait(generator, value) { |
- return %_Call( |
- PromiseThen, PromiseResolve(value), |
- function(sentValue) { |
- return %_Call(AsyncFunctionNext, generator, sentValue); |
- }, |
- function(sentError) { |
- return %_Call(AsyncFunctionThrow, generator, sentError); |
- }); |
+ // Promise.resolve(value).then( |
+ // value => AsyncFunctionNext(value), |
+ // error => AsyncFunctionThrow(error) |
+ // ); |
+ var promise; |
+ if (IsPromise(value)) { |
+ promise = value; |
+ } else { |
+ var promiseCapability = NewPromiseCapability(GlobalPromise); |
+ %_Call(promiseCapability.resolve, UNDEFINED, value); |
+ promise = promiseCapability.promise; |
+ } |
+ |
+ var onFulfilled = |
+ (sentValue) => %_Call(AsyncFunctionNext, generator, sentValue); |
+ var onRejected = |
+ (sentError) => %_Call(AsyncFunctionThrow, generator, sentError); |
+ |
+ var throwawayCapability = NewPromiseCapability(GlobalPromise); |
+ return PerformPromiseThen(promise, onFulfilled, onRejected, |
+ throwawayCapability); |
} |
%InstallToContext([ "async_function_await", AsyncFunctionAwait ]); |