等待带有内部回调的同步函数
Waiting for a synchronous function with internal callback
我有几个同步函数,其中一些不是我自己的代码。
这些函数在完成时接受并执行回调函数。
我需要等到这些回调完成后退出应用程序。
我该如何强制执行?
const serverGracefulShutdown = () => {
logger.info('Signal received: Gracefully killing application');
httpServer.close(() => {
logger.info('Apollo server closed');
});
redis.quit(() => {
logger.info('Redis client closed');
});
stopKafkaConsumer(() => {
logger.info('Kafka consumer closed');
});
logger.info('Application closed');
process.exit(0);
};
process.on('SIGINT', serverGracefulShutdown);
process.on('SIGTERM', serverGracefulShutdown);
在上面的代码中,我想确保应用程序正常关闭。
所有 运行 东西都停止了,然后我可以调用 process.exit(0).
您应该能够玩转 promise:
const serverGracefulShutdown = () => {
logger.info('Signal received: Gracefully killing application');
const promises = [
new Promise((resolve, reject) => httpServer.close(() => {
// do you get an optional error param inside this callback?
// if yes, then "if (error) reject(error) else resolve()"
// (or any other way to mark this operation as failed)
logger.info('Apollo server closed');
resolve();
})),
new Promise((resolve /* + reject? */) => redis.close(() => {
logger.info('Redis client closed');
resolve();
})),
new Promise((resolve /* + reject? */) => stopKafkaConsumer(() => {
logger.info('Kafka consumer closed');
resolve();
}))
];
return Promise.all(promises).then(() => {
logger.info('Application closed');
process.exit(0);
}).catch((error) => {
// what's your plan if at least one failed?
});
};
process.on('SIGINT', serverGracefulShutdown);
process.on('SIGTERM', serverGracefulShutdown);
我有几个同步函数,其中一些不是我自己的代码。 这些函数在完成时接受并执行回调函数。
我需要等到这些回调完成后退出应用程序。 我该如何强制执行?
const serverGracefulShutdown = () => {
logger.info('Signal received: Gracefully killing application');
httpServer.close(() => {
logger.info('Apollo server closed');
});
redis.quit(() => {
logger.info('Redis client closed');
});
stopKafkaConsumer(() => {
logger.info('Kafka consumer closed');
});
logger.info('Application closed');
process.exit(0);
};
process.on('SIGINT', serverGracefulShutdown);
process.on('SIGTERM', serverGracefulShutdown);
在上面的代码中,我想确保应用程序正常关闭。
所有 运行 东西都停止了,然后我可以调用 process.exit(0).
您应该能够玩转 promise:
const serverGracefulShutdown = () => {
logger.info('Signal received: Gracefully killing application');
const promises = [
new Promise((resolve, reject) => httpServer.close(() => {
// do you get an optional error param inside this callback?
// if yes, then "if (error) reject(error) else resolve()"
// (or any other way to mark this operation as failed)
logger.info('Apollo server closed');
resolve();
})),
new Promise((resolve /* + reject? */) => redis.close(() => {
logger.info('Redis client closed');
resolve();
})),
new Promise((resolve /* + reject? */) => stopKafkaConsumer(() => {
logger.info('Kafka consumer closed');
resolve();
}))
];
return Promise.all(promises).then(() => {
logger.info('Application closed');
process.exit(0);
}).catch((error) => {
// what's your plan if at least one failed?
});
};
process.on('SIGINT', serverGracefulShutdown);
process.on('SIGTERM', serverGracefulShutdown);