Skip to content

Commit 7807ad0

Browse files
author
John Haley
committed
Rewrote synchronous inspection code
Pulling in the latest from 'then/promise' forced a re-write of the synchronous inspection code.
1 parent 2de5045 commit 7807ad0

File tree

1 file changed

+28
-1
lines changed

1 file changed

+28
-1
lines changed

src/core.js

+28-1
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,33 @@ Promise.prototype._finale = function () {
140140
this._deferreds = null
141141
}
142142

143+
/**
144+
* Synchronous Inspection
145+
*/
146+
Promise.prototype.isPending = function () {
147+
return this._state === 0;
148+
}
149+
Promise.prototype.isFulfilled = function () {
150+
return this._state === 1 || this._state === 3;
151+
}
152+
Promise.prototype.isRejected = function () {
153+
return this._state === 2;
154+
}
155+
Promise.prototype.value = function () {
156+
if (!self.isFulfilled()) {
157+
throw new Error('Cannot get a value of an unfulfilled promise.')
158+
}
159+
160+
return this._value;
161+
}
162+
Promise.prototype.reason = function () {
163+
if (!self.isRejected()) {
164+
throw new Error('Cannot get a rejection reason of a non-rejected promise.')
165+
}
166+
167+
return this._value;
168+
}
169+
143170

144171
function Handler(onFulfilled, onRejected, promise){
145172
this.onFulfilled = typeof onFulfilled === 'function' ? onFulfilled : null
@@ -168,4 +195,4 @@ function doResolve(fn, promise) {
168195
done = true
169196
promise._reject(LAST_ERROR)
170197
}
171-
}
198+
}

0 commit comments

Comments
 (0)