Search code examples
javascripttimerpromisesettimeoutes6-promise

setTimeout inside Promise Race immediately catch


I have 2 promise, a fetch and a setTimeout, fetch wait for 5 seconds and setTimeout wait 4 seconds.

I don't known why but setTimeout fire immediately!

myService( args )
    .then( result => {
        console.log(result);
    })  
    .catch( timeoutError => {  // <-----------------  FIRE IMMEDIATELY
        console.error(timeoutError);
    })
;


function myService( args ){
    return Promise.race([
        fetch("http://localhost/wait-ten-seconds"),
        new Promise( (_, timeexpired) => {
            return setTimeout( function() {
                console.log("timeout");
                timeexpired({msg: "call timeout", code: 100, data: null});
            }
            , 4000)
        })
    ]);
}


Solution

  • I solve it in this way:
    I create a function that simulate timeout

    let activeTimer = null;
    let checkTimer = (startTimer, timeExpired) => {
        activeTimer = setTimeout(() => {
            if( (new Date()).getTime() - startTimer < TIMEOUT_DURATION_MS ) checkTimer(startTimer, timeExpired);
            else timeExpired( errorMessage.TIMEOUT_EXPIRED );
        }, 1000);
    };
    
    Promise.race([
        new Promise( (httpResponse, httpError) => {
            return fetch(url)
                .then(response => {
                    clearTimeout( activeTimer );
                    activeTimer = null;
                    httpResponse( response );
                })
                .catch( error => {
                    httpError( error );
                })
            })
            ,
            new Promise( (_, timeExpired) => {
                checkTimer((new Date()).getTime(), timeExpired);
            })
    ]);