mirror of
https://github.com/BreizhHardware/memoizee.git
synced 2026-01-18 16:37:21 +01:00
Improve promise value resolution
This commit is contained in:
@@ -2,7 +2,8 @@
|
||||
|
||||
'use strict';
|
||||
|
||||
var nextTick = require('next-tick')
|
||||
var isPromise = require('is-promise')
|
||||
, nextTick = require('next-tick')
|
||||
|
||||
, create = Object.create;
|
||||
|
||||
@@ -11,17 +12,19 @@ require('../lib/registered-extensions').promise = function (ignore, conf) {
|
||||
|
||||
// After not from cache call
|
||||
conf.on('set', function (id, ignore, promise) {
|
||||
promise.then(function () {
|
||||
// nextTick avoids error interception
|
||||
nextTick(function () {
|
||||
cache[id] = promise;
|
||||
conf.emit('setasync', id, 1);
|
||||
});
|
||||
}, function () {
|
||||
nextTick(function () {
|
||||
conf.delete(id);
|
||||
});
|
||||
});
|
||||
if (!isPromise(promise)) return; // Non promise result, ignore;
|
||||
var onSuccess = function () {
|
||||
cache[id] = promise;
|
||||
conf.emit('setasync', id, 1);
|
||||
};
|
||||
var onFailure = function () { conf.delete(id); };
|
||||
if (typeof promise.done === 'function') {
|
||||
// Optimal promise resolution
|
||||
promise.done(onSuccess, onFailure);
|
||||
} else {
|
||||
// Be sure to escape error swallowing
|
||||
promise.then(function () { nextTick(onSuccess); }, function () { nextTick(onFailure); });
|
||||
}
|
||||
});
|
||||
|
||||
// From cache (sync)
|
||||
|
||||
@@ -29,6 +29,7 @@
|
||||
"es5-ext": "~0.10.11",
|
||||
"es6-weak-map": "~0.1.4",
|
||||
"event-emitter": "~0.3.4",
|
||||
"is-promise": "~2.1",
|
||||
"lru-queue": "0.1",
|
||||
"next-tick": "~0.2.2",
|
||||
"timers-ext": "0.1"
|
||||
|
||||
Reference in New Issue
Block a user