cc = (resolve, reject) => {
const timeOut = 5
setTimeout(function () {
if (timeOut < 1) {
console.log('call resolve()...');
resolve('200 OK');
}
else {
console.log('call reject()...');
reject('timeout in ' + timeOut + ' seconds.');
}
}, timeOut * 1000);
}
clickFunction = () => {
new Promise(this.cc).then(data => {
alert(data)
}).catch(function (reason) {
console.log('Failed: ' + reason);
});
}
<button onClick={this.test}>a</button>
本文介绍了一个使用Promise实现的超时处理函数。通过设置超时时间,该函数能在指定时间内完成则调用resolve,否则调用reject,展示了如何在JavaScript中优雅地处理异步操作的超时情况。
2万+

被折叠的 条评论
为什么被折叠?



