Promises: make null a legal argument for .then
authorrossberg@chromium.org <rossberg@chromium.org@ce2b1a6d-e550-0410-aec6-3dcde31c8c00>
Tue, 18 Mar 2014 14:55:43 +0000 (14:55 +0000)
committerrossberg@chromium.org <rossberg@chromium.org@ce2b1a6d-e550-0410-aec6-3dcde31c8c00>
Tue, 18 Mar 2014 14:55:43 +0000 (14:55 +0000)
R=svenpanne@chromium.org
BUG=

Review URL: https://codereview.chromium.org/203453002

git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@20037 ce2b1a6d-e550-0410-aec6-3dcde31c8c00

src/promise.js
test/mjsunit/es6/promises.js

index f700be9..f6b7bdf 100644 (file)
@@ -210,7 +210,10 @@ function PromiseHandle(value, handler, deferred) {
 // Multi-unwrapped chaining with thenable coercion.
 
 function PromiseThen(onResolve, onReject) {
-  onResolve = IS_UNDEFINED(onResolve) ? PromiseIdResolveHandler : onResolve;
+  onResolve =
+    IS_NULL_OR_UNDEFINED(onResolve) ? PromiseIdResolveHandler : onResolve;
+  onReject =
+    IS_NULL_OR_UNDEFINED(onReject) ? PromiseIdRejectHandler : onReject;
   var that = this;
   var constructor = this.constructor;
   return this.chain(
@@ -230,11 +233,10 @@ function PromiseCoerce(constructor, x) {
     var then;
     try {
       then = x.then;
-    } catch(e) {
-      var deferred = %_CallFunction(constructor, PromiseDeferred);
-      PromiseCoerce.table.set(x, deferred.promise);
-      deferred.reject(e);
-      return deferred.promise;
+    } catch(r) {
+      var promise = %_CallFunction(constructor, r, PromiseRejected);
+      PromiseCoerce.table.set(x, promise);
+      return promise;
     }
     if (typeof then === 'function') {
       if (PromiseCoerce.table.has(x)) {
@@ -244,8 +246,8 @@ function PromiseCoerce(constructor, x) {
         PromiseCoerce.table.set(x, deferred.promise);
         try {
           %_CallFunction(x, deferred.resolve, deferred.reject, then);
-        } catch(e) {
-          deferred.reject(e);
+        } catch(r) {
+          deferred.reject(r);
         }
         return deferred.promise;
       }
index b513793..f1b191c 100644 (file)
@@ -100,7 +100,12 @@ function assertAsyncDone(iteration) {
 
 (function() {
   Promise.resolve(5).then(undefined, assertUnreachable).chain(
-    function(x) { assertAsync(x === 5, "resolved/then-nohandler") },
+    function(x) { assertAsync(x === 5, "resolved/then-nohandler-undefined") },
+    assertUnreachable
+  )
+  assertAsyncRan()
+  Promise.resolve(6).then(null, assertUnreachable).chain(
+    function(x) { assertAsync(x === 6, "resolved/then-nohandler-null") },
     assertUnreachable
   )
   assertAsyncRan()
@@ -109,7 +114,12 @@ function assertAsyncDone(iteration) {
 (function() {
   Promise.reject(5).then(assertUnreachable, undefined).chain(
     assertUnreachable,
-    function(r) { assertAsync(r === 5, "rejected/then-nohandler") }
+    function(r) { assertAsync(r === 5, "rejected/then-nohandler-undefined") }
+  )
+  assertAsyncRan()
+  Promise.reject(6).then(assertUnreachable, null).chain(
+    assertUnreachable,
+    function(r) { assertAsync(r === 6, "rejected/then-nohandler-null") }
   )
   assertAsyncRan()
 })();