0.1.15 • Published 5 years ago
@instaffogmbh/promise-timeout-with-warning v0.1.15
@instaffogmbh/promise-timeout-with-warning
Limit how long to wait for a promise to resolve, with optional earlier warning.
- 📣 💼 💰 Looking for a tech job? Try our reverse recruiting service.
API
This module exports one function:
ptww(origPr, opts)
Returns a promise tmoPr that reflects origPr's resolution or,
if that takes too long, is rejected with a timeout error.
origPr can be any value, e.g. a promise.
If it's not already a promise, ptww will instead use a new promise
resolved with that value.
opts is an optional options object that supports these keys:
warn,fail: Each either strings with a human-readable timeout specification like"30 sec", orfalseto deactivate that feature.unwatched: Ifwarnandfailare both deactivated (i.e. no timers would be installed) andunwatchedis set to sth. other thanundefined, immediately returnunwatchedinstead of dealing with any promise logic.autostart: If set tofalse,ptwwwon't start the timers immediately.logger: Anconsole-like object to use for logging. Default:consolewarnMsg: A message to be logged when thewarntimeout has elapsed.failMsg: Error message for the timeout promise rejection.msg: A message describing what still not happened yet. Used to extemporizewarnMsgand/orfailMsgif they're missing.descr: If nomsgwas provided, extemporize one from this noun.startMsg: If true-y, log this message when timers are (re-)started. Booleantrue= extemporize frommsg.doneMsg: If true-y, log this message whenorigPrsolves duly. Booleantrue= extemporize frommsg.vErr: If set,msgmust be a non-empty string.true: wrap any rejections except TimeoutError in a VError usingmsg."announce": liketruebut also extemporizestartMsganddoneMsgin case they're false-y.
errorSubj: If true-y, prefix non-timeout error messages with this topic/context description and': '.subj: LikeerrorSubjbut also affects the…Msgoption values.subjSuffix: In casesubjis used and this is neither set to the empty string nor false, put this betweensubjand the message. (Does not affecterrorSubj.) Defaults to': '(a colon and a space).
tmoPr will expose these additional methods:
ifWatching(): ReturnstmoPrif any of the timers is currently installed, orfalseotherwise.reset(): Restart both timers. If you were warned already, you might be warned again.unTimeout(): Uninstall and abandon both timers. You can install new ones with.reset(). ReturnstmoPr.
License
MIT