Last active
October 21, 2020 14:05
-
-
Save Dunkelheit/a055cfdca83b62a81ff75505c5d663e5 to your computer and use it in GitHub Desktop.
pending-promise-recycler-express - part 1
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.log('>>> Incoming 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.log('<<< Outgoing response'); | |
}) | |
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