@instaffogmbh/promise-timeout-with-warning
v0.1.15
Published
Limit how long to wait for a promise to resolve, with optional earlier warning.
Downloads
62
Readme
@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"
, orfalse
to deactivate that feature.unwatched
: Ifwarn
andfail
are both deactivated (i.e. no timers would be installed) andunwatched
is set to sth. other thanundefined
, immediately returnunwatched
instead of dealing with any promise logic.autostart
: If set tofalse
,ptww
won't start the timers immediately.logger
: Anconsole
-like object to use for logging. Default:console
warnMsg
: A message to be logged when thewarn
timeout has elapsed.failMsg
: Error message for the timeout promise rejection.msg
: A message describing what still not happened yet. Used to extemporizewarnMsg
and/orfailMsg
if they're missing.descr
: If nomsg
was 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 whenorigPr
solves duly. Booleantrue
= extemporize frommsg
.vErr
: If set,msg
must be a non-empty string.true
: wrap any rejections except TimeoutError in a VError usingmsg
."announce"
: liketrue
but also extemporizestartMsg
anddoneMsg
in case they're false-y.
errorSubj
: If true-y, prefix non-timeout error messages with this topic/context description and': '
.subj
: LikeerrorSubj
but also affects the…Msg
option values.subjSuffix
: In casesubj
is used and this is neither set to the empty string nor false, put this betweensubj
and the message. (Does not affecterrorSubj
.) Defaults to': '
(a colon and a space).
tmoPr
will expose these additional methods:
ifWatching()
: ReturnstmoPr
if any of the timers is currently installed, orfalse
otherwise.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