在Javascript中缓存和预取即将到期的承诺 [英] Caching and pre-fetching expiring promises in Javascript
问题描述
Promise是我在Javascript中管理异步代码的首选方式。
Memoize(npm上的memoizee)是一个用于轻松缓存的Javascript库。预取函数的结果。
Promises are my preferred way of managing my asynchronous code in Javascript. Memoize (memoizee on npm) is a Javascript library for easily caching & pre-fetching results of functions.
理想情况下,我希望结合两者的优点,并且能够使Promise过期并预先获取新的Promise结果(当触摸缓存并接近到期时)。 Memoize可以做到这一点,但它并没有考虑到Promise。
Ideally I want to combine the best of both, and have the ability to "expire" a Promise and pre-fetch a new Promise result (when the cache is touched and near to expiring). Memoize can do this, but it wasn't built with Promises in mind.
(我知道Promises有一个内置的永远缓存,因为它们的性质,但永远对于我的应用来说太长了
(I understand that Promises have a built-in "forever-cache" as is their nature, but forever is too long for my application)
到目前为止我做这个的最佳尝试如下(node.js示例使用< a href =https://github.com/petkaantonov/bluebird> bluebird , memoize 和下划线):
My best attempt to do this so far is as follows (node.js example using bluebird, memoize and underscore):
var memoize = require('memoizee')
, Promise = require('bluebird')
, _ = require('underscore')
;
// returns a memoized Promise-generating function
function memoizePromiser(funcReturningPromise, options) {
var opts = _.defaults(options || {}, {async: true});
return Promise.promisify(memoize(function(){
var args = _.initial(arguments);
var cb = _.last(arguments);
funcReturningPromise.apply(this, args).nodeify(cb);
}, opts));
};
这个函数允许我记忆一个异步函数(带有类似节点的回调)返回一个promise,同时还提供了Memoize库提供的所有缓存到期(+更多)功能。
This function will allow me to memoize an async function (with node-like callbacks) returning a promise, whilst also providing all the cache-expiry (+ more) functionality offered by the Memoize library.
然而,这种方法看起来很混乱,而且过度设计。我想我自己 - 这肯定是一个普遍的问题,而其他人已经提出了一个更优雅的解决方案吗?
However, this approach seems messy and over-engineered to me. I think to myself - surely this is a common enough problem hat someone else has made a more elegant solution?
这是我尝试做的最好的方法?还有另一个像memoize这样专门为promises设计的库吗?是否有一个我错过的更简单的解决方案?
Is this the best approach for what I'm trying to do? Is there another library like memoize which is designed specifically for promises? Is there a simpler solution that I've missed?
任何指导都将受到赞赏。
Any guidance would be appreciated.
推荐答案
这个函数允许我记忆一个异步函数(带有类似节点的回调)返回一个promise。然而,这种方法看起来很混乱并且过度设计了。
This function will allow me to memoize an async function (with node-like callbacks) returning a promise. However, this approach seems messy and over-engineered to me.
是的 - 你已经有一个函数返回一个promise,没有回到nodebacks的原因。
Yes - you already have a function returning a promise, there is no reason to go back to nodebacks.
我错过了一个更简单的解决方案吗?
Is there a simpler solution that I've missed?
返回promise的函数只是一个返回某个值的函数 - 一个可以通过 memoize缓存的值
功能。只需
A function that returns a promise is just a function that returns some value - a value that can be cached by the memoize
function. Simply do
memoize(funcReturningPromise, options)
这篇关于在Javascript中缓存和预取即将到期的承诺的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!