用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]);
}
网友评论