首先,我很抱歉我是JS的初学者。下面的代码停滞在第三行。在background_mine方法中。我不能访问这个方法,那么在这个方法中停留两分钟后如何重置代码呢
Mining = async () => {
console.log(`## rebalance: ${await getBalance(account, wax.api.rpc)}`);
let mine_work = await background_mine(account)
}发布于 2021-05-07 05:54:02
为了实现超时,您可以像这样使用Promise.race:
const background_mine = account => new Promise(() => {});
const promisifiedTimeout = new Promise((resolve, reject) => setTimeout(() => reject('Timed out'), 5000));
(async () => {
const account = {};
try {
const result = await Promise.race([background_mine(account), promisifiedTimeout]);
}
catch(e) {
console.log(e);
}
})();
发布于 2021-05-07 05:55:03
您可能需要执行Promise.race
正常工作的示例:
const background_mine = new Promise(function(resolve, reject) {
setTimeout(() => resolve('background_mine'), 500);
});
const limitChecker = new Promise(function(resolve, reject) {
setTimeout(() => resolve('background_failed'), 1000);
});
const result = await Promise.race([background_mine, limitChecker])
.catch((error) => {
console.log(`This timed out`);
});
console.log(result); //background_mine具有超时的示例:
const background_mine = new Promise(function(resolve, reject) {
setTimeout(() => resolve('background_mine'), 500);
});
const limitChecker = new Promise(function(resolve, reject) {
setTimeout(() => resolve('background_failed'), 1000);
});
const result = await Promise.race([background_mine, limitChecker])
.catch((error) => {
console.log(`This timed out`);
});
console.log(result); //undefined如果您检查,这完全取决于background_mine函数是低于还是高于limitChecker超时值。
https://stackoverflow.com/questions/67426305
复制相似问题