From cc8df90f8f8c2008c8bdc43fc9e256e73afd0100 Mon Sep 17 00:00:00 2001 From: Bryan Donovan Date: Mon, 23 May 2016 09:39:44 -0700 Subject: [PATCH] promise example --- examples/example.js | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/examples/example.js b/examples/example.js index 9d3b875..5abcc80 100644 --- a/examples/example.js +++ b/examples/example.js @@ -1,4 +1,8 @@ /*jshint unused:false*/ +if (!global.Promise) { + require('es6-promise').polyfill(); +} + // Note: ttls are in seconds var cacheManager = require('../'); var memoryCache = cacheManager.caching({store: 'memory', max: 100, ttl: 10}); @@ -130,3 +134,27 @@ multiCache.wrap(key2, function(cb) { }); }); }); + +// Promise example: +function getUserPromise(id) { + return new Promise(function(resolve, reject) { + getUser(id, function(err, result) { + if (err) { + return reject(err); + } + + return resolve(result); + }); + }); +} + +function getCachedUserPromise(id) { + memoryCache.wrap(key, function() { + return getUserPromise(userId); + }) + .then(function(user) { + console.log('User fetched via Promise:', user); + }); +} + +getCachedUserPromise('123');