Skip to content

Instantly share code, notes, and snippets.

@Dunkelheit
Last active October 21, 2020 14:05
Show Gist options
  • Save Dunkelheit/a055cfdca83b62a81ff75505c5d663e5 to your computer and use it in GitHub Desktop.
Save Dunkelheit/a055cfdca83b62a81ff75505c5d663e5 to your computer and use it in GitHub Desktop.
pending-promise-recycler-express - part 1
'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