对于循环,没有得到诺言的适当效力
我正在将项目从JavaScript转换为打字稿,并在失败系统上进行重试时遇到了一些问题。该系统在Vanilla JS上正常工作,但似乎在Typescript上的某个地方失败了。
var t: number = 500;
var max: number = 5;
const rejectDelay = (reason: string): Promise<any> => {
return new Promise((resolve, reject) => {
log.warn('Unable to connect to database. Trying again...');
setTimeout(() => reject.bind(null, reason), t);
});
};
const dbConn = async (): Promise<void> => {
let conn = await db.pool
.getConnection()
.then((message: any) => {
if (!message.isValid()) {
throw message.isValid;
} else {
return message.isValid;
}
});
};
const connected = (res: string): void => {
log.info('Connection to database is successful.');
log.info(`App is running in port ${envs.port} in ${envs.mode} mode.`);
};
const notConnected = async (err: any): Promise<void> => {
log.error('Max attempts exceeded, app will exit...');
process.exit(1);
};
var p: Promise<any> = Promise.resolve();
for (var i = 0; i < max; i++) {
console.log(i);
p = p.catch(dbConn).catch(rejectDelay);
}
p = p.then(connected).catch(notConnected);
运行项目后,这是预期的结果:
[info] 2022-05-18 01:13:03: Router users registered...
[info] 2022-05-18 01:13:03: Router utils registered...
0
[warn] 2022-05-18 01:12:59: Unable to connect to database. Trying again...
1
[warn] 2022-05-18 01:12:59: Unable to connect to database. Trying again
2
[warn] 2022-05-18 01:12:59: Unable to connect to database. Trying again
3
[warn] 2022-05-18 01:12:59: Unable to connect to database. Trying again
4
[error] 2022-05-18 01:13:44: Max attempts exceeded, app will exit...
这就是我得到的:
[info] 2022-05-18 01:13:03: Router users registered...
[info] 2022-05-18 01:13:03: Router utils registered...
0
1
2
3
4
[error] 2022-05-18 01:13:44: Max attempts exceeded, app will exit...
有什么建议吗?
I've being converting a project from Javascript to Typescript and encountered some problems when running a retry on fail system. This system works fine on vanilla JS but it seems to fail somewhere on Typescript.
var t: number = 500;
var max: number = 5;
const rejectDelay = (reason: string): Promise<any> => {
return new Promise((resolve, reject) => {
log.warn('Unable to connect to database. Trying again...');
setTimeout(() => reject.bind(null, reason), t);
});
};
const dbConn = async (): Promise<void> => {
let conn = await db.pool
.getConnection()
.then((message: any) => {
if (!message.isValid()) {
throw message.isValid;
} else {
return message.isValid;
}
});
};
const connected = (res: string): void => {
log.info('Connection to database is successful.');
log.info(`App is running in port ${envs.port} in ${envs.mode} mode.`);
};
const notConnected = async (err: any): Promise<void> => {
log.error('Max attempts exceeded, app will exit...');
process.exit(1);
};
var p: Promise<any> = Promise.resolve();
for (var i = 0; i < max; i++) {
console.log(i);
p = p.catch(dbConn).catch(rejectDelay);
}
p = p.then(connected).catch(notConnected);
after running the project this is expected result:
[info] 2022-05-18 01:13:03: Router users registered...
[info] 2022-05-18 01:13:03: Router utils registered...
0
[warn] 2022-05-18 01:12:59: Unable to connect to database. Trying again...
1
[warn] 2022-05-18 01:12:59: Unable to connect to database. Trying again
2
[warn] 2022-05-18 01:12:59: Unable to connect to database. Trying again
3
[warn] 2022-05-18 01:12:59: Unable to connect to database. Trying again
4
[error] 2022-05-18 01:13:44: Max attempts exceeded, app will exit...
and this is what I get:
[info] 2022-05-18 01:13:03: Router users registered...
[info] 2022-05-18 01:13:03: Router utils registered...
0
1
2
3
4
[error] 2022-05-18 01:13:44: Max attempts exceeded, app will exit...
Any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您希望它停止在第一个连接上,并且希望它在再次尝试连接失败之前等待超时,则需要其他方法。这是一种方法(请注意,您可能必须修复某些打字稿的内容,因为我是普通的JS开发人员,而不是打字稿):
要尝试自己调试,我添加了一堆调试并进行了操作,以便它运行nodejs中的独立。您可以将其复制到单独的文件并自行运行,并查看其工作原理。我已经制作了
dbConn()
,使其在前3次失败,然后第四次成功,因此您可以看到它的工作原理:If you want it to stop on the first connection and you want it to wait a timeout before trying again upon a connection failure, then you need a different approach. Here's one way to do it (note you may have to fix up some TypeScript stuff as I'm a plain JS developer, not TypeScript):
To attempt to debug this myself, I added a bunch of debugging and made it so it would run stand-alone in nodejs. You can copy this to a separate file and run it by itself and see how it works. I've made
dbConn()
such that it fails the first 3 times, then succeeds the fourth time so you can see how it works: