Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Jan Küster 🔥
Jan Küster 🔥

Posted on

     

Create a JavaScript Promise with timeout

I found a few examples on the web that let Promises run with a timeout:

Both were already good ideas but I wanted some more control of their behaviour and therefore I want to share my modified version.

Code

constdefaultMessage='promise.timedOut'/** * Lets a promise race against a timeout. If the promise settles before the timeout then it will * be the one to use (no matter, whether it has been resolved or rejected). * * Otherwise the timeout promise will be used to either resolve to a message * or reject an error, depending on the used options. * * @param promise {Promise} the promise to race against the timeout * @param timeout {number=}  optional number of milliseconds until timeout, defaults to 1000ms / 1 sec * @param throwIfTimedOut {boolean=} optional flag to either reject (if true) or resolve (if false), defaults to false * @param message {string=} optional message to be resolved/rejected on timeout, defaults to 'promise.timedOut' * @return {Promise<Awaited<unknown>>} */exportconstcreateTimedPromise=(promise,{timeout=1000,throwIfTimedOut=false,message,details}={})=>{lettimeOut=undefinedconstrace=Promise.race([promise,newPromise((resolve,reject)=>{timeOut=setTimeout(()=>{if(throwIfTimedOut){consterror=newError(message||defaultMessage)// I often like to attach contextual information to errors.// This is up to you, whether to do this or noterror.details=detailsreturnreject(error)}else{returnresolve(message||defaultMessage)}},timeout)})])// always clear timeout to prevent weird behaviourrace.finally(()=>clearTimeout(timeOut))returnrace}
Enter fullscreen modeExit fullscreen mode

Usage

// all defaultawaitcreateTimedPromise(newPromise((resolve,reject)=>{...}))// custom timeoutawaitcreateTimedPromise(newPromise((resolve,reject)=>{...}),{timeout:500})// reject if timedoutawaitcreateTimedPromise(newPromise((resolve,reject)=>{...}),{throwIfTimedOut:true})// additional contextawaitcreateTimedPromise(newPromise((resolve,reject)=>{...}),{message:'you lost',details:{foo:'bar'}})
Enter fullscreen modeExit fullscreen mode

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Graduated in Digital Media M.Sc. now developing the next generation of educational software. Since a while I develop full stack in Javascript using Meteor. Love fitness and Muay Thai after work.
  • Location
    Bremen, Germany
  • Education
    M.Sc.
  • Pronouns
    he/him
  • Work
    Scientific Employee at University of Bremen
  • Joined

More fromJan Küster 🔥

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp