let runPromiss = (name, time, status) => {
return new Promise((resolve, reject) => {
if(status) {
console.log(`${name} 起跑了!`)
return setTimeout(() => {
resolve(`${name} 衝了 ${time / 1000}秒`)
},time);
} else {
reject(new Error(`${name} 出現錯誤`));
};
});
};
// 小明失敗,就回傳錯誤訊息
let min = await runPromiss('小明', 3000, true);
console.log(min);
let wei = await runPromiss('小王', 1000, true)
console.log(wei);
console.log('我偷偷執行了');
This Pen doesn't use any external CSS resources.
This Pen doesn't use any external JavaScript resources.