Question

Is it an acceptable (not surprising) to use promises to cache results? The idea is to generate a promise once, and just return that same promise again on subsequent calls.

For example, a getAll() function that returns a promise would perform a time-intensive function only on the first call, and then return the same promise on repeat calls.

Example (in JavaScript, but I didn't actually try it, so more like pseudo-code):

var oldPromise = null;

function getAll() {
    var newPromise;

    if (!oldPromise) {

        // first time called
        newPromise = new Promise(function (resolve, reject) {
            timeIntensiveFunction(function callback(data) {
                resolve(data);
            });
        });

        oldPromise = newPromise

        return newPromise;
    } else {

        // already did it
        return oldPromise;
    }
}
Was it helpful?

Solution

This is an absolutely sensible use of promises. Its one of the primary ways that promises are intended to be used.

The way that promises can have .then() added before or after being resolved is directly to allow this kind of usage. The point is that you might have the value right away, or it might not show up until some time later. In either case, the promise api runs the same way.

On the side, your code can be simplified

var promise = null;

function getAll() {
    if (!promise) {

        // first time called
        promise = new Promise(function (resolve, reject) {
            timeIntensiveFunction(function callback(data) {
                resolve(data);
            });
        });
    }
    return promise;
}

Or using lodash or underscore

getAll = _.memoize(function() {
   return new Promise(function (resolve, reject) {
       timeIntensiveFunction(function callback(data) {
           resolve(data);
       });
   });   
});

OTHER TIPS

Well, calling a time intensive function within asynchronous flow is not acceptable because that blocks everything until it returns. That's the fundamental performance problem with cooperative multi-tasking.

Secondly, be sure to test possible flows with your actual promise library. Specifically test what happens if the promise is returned twice, and starts running twice, and tries to finish twice. Different promise libraries may do different things in this situation. The safe solution is to assume that it is up to you to avoid actually running the long thing twice, so you do not attempt to resolve twice.

Third, your snippet does not show where data arrives from. Be sure not to return the results of computing with the wrong set of data!

Fourth, if in doubt, comment on the purpose of what you are doing. Don't confuse the maintenance programmer!

Those are a lot of caveats. If you're comfortable with them, then this does seem to be a reasonable solution. I'd personally be inclined to more up front caching mechanisms which make it obvious what is going on. But this should work.

Licensed under: CC-BY-SA with attribution
scroll top