Created
July 5, 2018 19:10
-
-
Save mdlavin/e89cfcc68692c4766e4aa3198cf6ae37 to your computer and use it in GitHub Desktop.
A helper to make sure that a Promise will always resolve
This file contains 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
function safePromise (promise, timeout) { | |
let slowOpId; | |
const slowLoggingPromise = pFinally(promise, function () { | |
if (slowOpId) { | |
log.warn({slowOpId, itemKey}, 'The Promise eventually resolved'); | |
} | |
}); | |
// Don't wait on longer than `timeout` ms for the Promise | |
const promiseWithTimeout = pTimeout(slowLoggingPromise, timeout, function () { | |
slowOpId = uuid(); | |
log.warn({slowOpId, itemKey}, 'The Promise was slower than expected'); | |
return undefined; | |
}); | |
return promiseWithTimeout; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment