Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The implementation does not pass promise/A+ test suit #57

Open
neewbee opened this issue Nov 13, 2019 · 0 comments
Open

The implementation does not pass promise/A+ test suit #57

neewbee opened this issue Nov 13, 2019 · 0 comments

Comments

@neewbee
Copy link

neewbee commented Nov 13, 2019

2.2.2.2: it must not be called before `promise` is fulfilled
      1) fulfilled after a delay

2.2.3.2: it must not be called before `promise` is rejected
      2) rejected after a delay

2.3.1: If `promise` and `x` refer to the same object, reject `promise` with a `TypeError' as the reason.
    3) via return from a fulfilled promise
    4) via return from a rejected promise

below is my salution

diff --git a/Promise-origin.js b/Promise.js
index 57c1bb1..0a17c3b 100644
--- a/Promise-origin.js
+++ b/Promise.js
@@ -14,21 +14,25 @@ function Promise(fn) {
   var handlers = [];
 
   function fulfill(result) {
-    if (state === PENDING) {
-      state = FULFILLED;
-      value = result;
-      handlers.forEach(handle);
-      handlers = null;
-    }
+    setTimeout(function() {
+      if (state === PENDING) {
+        state = FULFILLED;
+        value = result;
+        handlers.forEach(handle);
+        handlers = null;
+      }
+    });
   }
 
   function reject(error) {
-    if (state === PENDING) {
-      state = REJECTED;
-      value = error;
-      handlers.forEach(handle);
-      handlers = null;
-    }
+    setTimeout(function() {
+      if (state === PENDING) {
+        state = REJECTED;
+        value = error;
+        handlers.forEach(handle);
+        handlers = null;
+      }
+    });
   }
 
   function resolve(result) {
@@ -73,7 +77,9 @@ function Promise(fn) {
         function(result) {
           if (typeof onFulfilled === 'function') {
             try {
-              return resolve(onFulfilled(result));
+              var x = onFulfilled(result);
+              if (x === promise) reject(new TypeError('same object'));
+              return resolve(x);
             } catch (ex) {
               return reject(ex);
             }
@@ -84,7 +90,9 @@ function Promise(fn) {
         function(error) {
           if (typeof onRejected === 'function') {
             try {
-              return resolve(onRejected(error));
+              var x = onRejected(error);
+              if (x === promise) reject(new TypeError('same object'));
+              return resolve(x);
             } catch (ex) {
               return reject(ex);
             }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant