У меня есть следующее хранилище кеша:
const BPromise = require('bluebird');
const LRU = require('lru-cache');
const product_cache = new LRU(5000);
function getCache(cacheName) {
switch (cacheName) {
case 'product_cache':
return BPromise.resolve(product_cache);
default:
return BPromise.resolve(new LRU(5000));
}
}
function set(id, uuid, cacheName) {
return getCache(cacheName).then(function(cache) {
return BPromise.resolve(cache.set(id,uuid));
});
}
function get(id, cacheName) {
return getCache(cacheName).then(function(cache) {
return BPromise.resolve(cache.get(id));
});
}
module.exports = {
set: set,
get: get,
};
Я звоню так:
let p = new BPromise(function(resolve, reject){
if (use_cache) {
return resolve(id_to_uuid_cache.get(id, cacheName));
} else {
return resolve(null);
}
});
let uuid = p;
if (uuid) {
result.set(id, uuid);
} else {
unknown_ids.push(id);
}
однако, когда обещание входит в вызов id_to_uuid_cache.get(id, cacheName)
оно входит во внутреннюю цепочку обещаний
return getCache(cacheName).then(function(cache) {
return BPromise.resolve(cache.get(id));
});
но как только он достигнет линии:
return BPromise.resolve(product_cache);
оно выпрыгивает из обещания выстроиться в линию let uuid = p;
как я могу убедиться, что завершил цепочку обещаний, прежде чем перейти к обещанию.