Created
October 21, 2020 14:01
-
-
Save Dunkelheit/e85d764518dffc8e5d1f09d616f0db5e to your computer and use it in GitHub Desktop.
pending-promise-recycler-express - part 2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
const express = require('express'); | |
const recycle = require('pending-promise-recycler'); | |
const server = express(); | |
const someSlowAndExpensiveOperation = function () { | |
return new Promise((resolve, reject) => { | |
console.log('Starting a very slow operation!') | |
setTimeout(() => { | |
console.log('Slow operation is over!') | |
resolve({ foo: 'bar' }); | |
}, 1500); | |
}); | |
} | |
server.get('/', async function (req, res) { | |
console.time('Request'); | |
const recycledOperation = recycle(someSlowAndExpensiveOperation, { | |
keyBuilder: 'slow-and-expensive' // Optionally define your own mechanism to uniquely identify functions | |
}); | |
const data = await recycledOperation(); | |
res.json(data); | |
console.timeEnd('Request'); | |
}) | |
server.listen(3000, () => { | |
console.log('Express server up and running!'); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment