How can I memoize a promise-based function?
Would straightforward memoization of the function suffice?
function foo() {
return new Promise((reso
For promises simple sync memoize will not be good, because in most of cases you will not wish to memoize errors (rejected promises).
I did a simple library for common needs: https://github.com/nodeca/promise-memoize
Pseudo code:
let db = require('mongoose').createConnection('mongodb://localhost/forum');
function lastPosts(limit) {
return db.model('Post').find()
.limit(limit).orderBy('-_id').lean(true).exec(); // <- Promise (thenable)
}
let cachedLastPosts = require('promise-memoize')(lastPosts, { maxAge: 60000 });
// Later...
cachedLastPosts(10).then(posts => console.log(posts));