Comparing version 0.4.0 to 0.4.1
@@ -14,9 +14,2 @@ // Support for functions returning promise | ||
// We may want to force 'then' usage, when promise implementation that's used: | ||
// - Implements both `done` and `finally` | ||
// - For rejected promise throws rejection reason unconditionally when `done` is called with | ||
// no onFailue callback (even though some other `then` or `done` call may have processed the | ||
// error) | ||
var forceThenMode = (mode === 'then'); | ||
// After not from cache call | ||
@@ -46,27 +39,23 @@ conf.on('set', function (id, ignore, promise) { | ||
// Use 'finally' and not rejection callback (on 'done' or 'then') to not register error | ||
// handling. | ||
// Additionally usage of 'finally' should take place if implementation | ||
// supports promise cancelation (then no `then` or `done` callbacks are invoked) | ||
var hasFinally = (typeof promise.finally === 'function'); | ||
if (!forceThenMode && (typeof promise.done === 'function')) { | ||
if ((mode !== 'then') && (typeof promise.done === 'function')) { | ||
// Optimal promise resolution | ||
if (hasFinally) { | ||
if ((mode !== 'done') && (typeof promise.finally === 'function')) { | ||
// Use 'finally' to not register error handling (still proper behavior is subject to | ||
// used implementation, if library throws unconditionally even on handled errors | ||
// switch to 'then' mode) | ||
promise.done(onSuccess); | ||
promise.finally(onFailure); | ||
} else { | ||
// With no `finally` side effect is that it mutes any eventual | ||
// "Unhandled error" events on returned promise | ||
promise.done(onSuccess, onFailure); | ||
} | ||
} else { | ||
// Be sure to escape error swallowing | ||
if (hasFinally) { | ||
promise.then(function (result) { nextTick(onSuccess.bind(this, result)); }); | ||
promise.finally(function () { nextTick(onFailure); }); | ||
} else { | ||
promise.then(function (result) { | ||
nextTick(onSuccess.bind(this, result)); | ||
}, function () { | ||
nextTick(onFailure); | ||
}); | ||
} | ||
// With no `done` it's best we can do. | ||
// Side effect is that it mutes any eventual "Unhandled error" events on returned promise | ||
promise.then(function (result) { | ||
nextTick(onSuccess.bind(this, result)); | ||
}, function () { | ||
nextTick(onFailure); | ||
}); | ||
} | ||
@@ -73,0 +62,0 @@ }); |
{ | ||
"name": "memoizee", | ||
"version": "0.4.0", | ||
"version": "0.4.1", | ||
"description": "Memoize/cache function results", | ||
@@ -5,0 +5,0 @@ "author": "Mariusz Nowak <medikoo@medikoo.com> (http://www.medikoo.com/)", |
@@ -155,5 +155,9 @@ # Memoizee | ||
Still relying on `done` may cause trouble if implementation that's used throws rejection reasons when `done` is called with no _onFail_ callback on rejected promise, even though error handler might have been registered through other `then` or `done` call. | ||
Still relying on `done` & `finally` pair, may cause trouble if implementation that's used throws rejection reasons when `done` is called with no _onFail_ callback, even though error handler might have been registered through other `then` or `done` call. | ||
If that's the case for you, please force usage of then by passing `'then'` string to `promise` option as: | ||
If that's the case for you, you can force to not use `finally` or `done` (even if implemented) by providing following value to `promise` option: | ||
- `'done'` - If `done` is implemented, it will purely try use `done` to register internal callbacks and not `finally` (even if it's implemented). If `done` is not implemented, this setting has no effect and callbacks are registered via `then`. | ||
_This mode comes with side effect of silencing eventual 'Unhandled errors' on returned promise_ | ||
- `'then'` - No matter if `done` and `finally` are implemented, internal callbacks will be registered via `then`. | ||
_This mode comes with side effect of silencing eventual 'Unhandled errors' on returned promise_ | ||
@@ -160,0 +164,0 @@ ```javascript |
@@ -93,3 +93,3 @@ 'use strict'; | ||
mfn = memoize(fn, { promise: true, dispose: a.never }); | ||
mfn = memoize(fn, { promise: 'done', dispose: a.never }); | ||
@@ -194,3 +194,3 @@ mfn(3, 7).done(a.never, function (err) { | ||
mfn = memoize(fn, { promise: true, primitive: true }); | ||
mfn = memoize(fn, { promise: 'done', primitive: true }); | ||
@@ -197,0 +197,0 @@ mfn(3, 7).done(a.never, function (err) { |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
175214
425
4890