timeoutHelper.mjs 396 B

123456789101112131415161718
  1. async function processWithTimeout({ work, timeout, message }) {
  2. let workDeadLine
  3. function checkInResults() {
  4. clearTimeout(workDeadLine)
  5. }
  6. await Promise.race([
  7. new Promise((resolve, reject) => {
  8. workDeadLine = setTimeout(() => {
  9. reject(new Error(message))
  10. }, timeout)
  11. }),
  12. work.finally(checkInResults),
  13. ])
  14. }
  15. module.exports = {
  16. processWithTimeout,
  17. }