promise写delay

2020-07-12  本文已影响0人  Time_Notes

用promise写一个delay

function delay(ms){

    return new Promise(resolve=>

        setTimeout(resolve,ms);

    );

}

delay(100).then(()=>alert("过了100ms"))

用promise写一个timeout

function timeoutPromise(promise,ms){

    var timeout = delay(ms).then(()=>{

        reject(new Error('timed out after '+ms+' ms'))

    });

    return Promise.race([promise,timeout]);

}

上一篇下一篇

猜你喜欢

热点阅读