You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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);
}
The text was updated successfully, but these errors were encountered:
below is my salution
The text was updated successfully, but these errors were encountered: