Skip to content

Commit

Permalink
feat: improve internal promise validation
Browse files Browse the repository at this point in the history
  • Loading branch information
medikoo committed Aug 29, 2017
1 parent 759e315 commit d23b94f
Showing 1 changed file with 28 additions and 2 deletions.
30 changes: 28 additions & 2 deletions ext/promise.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* eslint max-statements: 0 */

// Support for functions returning promise

"use strict";
Expand Down Expand Up @@ -76,6 +78,12 @@ require("../lib/registered-extensions").promise = function (mode, conf) {
}
);
} else if (resolvedMode === "then:finally") {
if (typeof promise.finally !== "function") {
throw new Error(
"Memoizee error: Retrieved promise does not implement 'finally' " +
"in 'then:finally' mode"
);
}
promise.then(function (result) {
isSettled = true;
nextTick(onSuccess.bind(this, result));
Expand All @@ -86,10 +94,28 @@ require("../lib/registered-extensions").promise = function (mode, conf) {
});
} else if (resolvedMode === "done") {
// Not recommended, as it may mute any eventual "Unhandled error" events
if (typeof promise.done !== "function") {
throw new Error(
"Memoizee error: Retrieved promise does not implement 'done' " +
"in 'done' mode"
);
}
promise.done(onSuccess, onFailure);
} else if (resolvedMode === "done:finally") {
// Cleanest solution assuming library does not throw unconditionally for rejected
// promises. Otherwise then:finally mode should be sued
// The only mode with no side effects assuming library does not throw unconditionally
// for rejected promises. Otherwise then:finally mode should be used instead
if (typeof promise.done !== "function") {
throw new Error(
"Memoizee error: Retrieved promise does not implement 'done' " +
"in 'done:finally' mode"
);
}
if (typeof promise.finally !== "function") {
throw new Error(
"Memoizee error: Retrieved promise does not implement 'finally' " +
"in 'done:finally' mode"
);
}
promise.done(onSuccess);
promise.finally(onFailure);
}
Expand Down

0 comments on commit d23b94f

Please sign in to comment.