Skip to content

Commit 8202ed2

Browse files
committed
Fix unhandled rejection warnings in when.settle (cujojs#493)
* Fix unhandled rejection warnings that resulted from creating superfluous Thenables during optimizations in settleOne() when working with foreign Promise objects
1 parent 8a7785a commit 8202ed2

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

lib/decorators/array.js

+14-4
Original file line numberDiff line numberDiff line change
@@ -239,13 +239,23 @@ define(function(require) {
239239
}
240240

241241
function settleOne(p) {
242-
var h = Promise._handler(p);
243-
if(h.state() === 0) {
242+
// Optimize the case where we get an already-resolved when.js promise
243+
// by extracting its state:
244+
var handler;
245+
if (p instanceof Promise) {
246+
// This is our own Promise type and we can reach its handler internals:
247+
handler = p._handler.join();
248+
}
249+
if((handler && handler.state() === 0) || !handler) {
250+
// Either still pending, or not a Promise at all:
244251
return toPromise(p).then(state.fulfilled, state.rejected);
245252
}
246253

247-
h._unreport();
248-
return state.inspect(h);
254+
// The promise is our own, but it is already resolved. Take a shortcut.
255+
// Since we're not actually handling the resolution, we need to disable
256+
// rejection reporting.
257+
handler._unreport();
258+
return state.inspect(handler);
249259
}
250260

251261
/**

0 commit comments

Comments
 (0)