javascript - Promise.all not awaiting for the promises?
问题描述
This function takes 2 asynchronous callbacks. I am not sure why, but when these callbacks are called, the promises they await aren't being awaited correctly. I'm thinking it may have something to do with the way I'm calling them. I am not very familiar with the promise API so I kind of just hacked this together. If someone could tell me if I am doing something wrong, I would really appreciate it.
async queryTasks(handleCommand, handleSubmission) {
const D = new Date().getTime();
const promises = [];
while (!this.tasks.isEmpty()) {
const task = this.tasks.dequeue();
// If not a submission
if (task.item.body) {
const command = new Command().test(task.item.body);
if (command) { // If the item received was a command, return the command, the item, and priority
const T = {
command: command,
item: task.item,
priority: task.priority,
time: D
}
console.log("Calling back with handleCommand(task)".bgMagenta.white);
promises.push(handleCommand(T));
}
} else if (task.item.title) { // Task was a submission
console.log("Calling back with handleSubmission".bgMagenta.black);
const T = {
item: task.item,
priority: task.priority,
time: D
}
promises.push(handleSubmission(T));
}
}
return Promise.all(promises);
}
Or maybe it's the way I'm calling it queryTasks()?
/* [Snoolicious Run Cycle] */
const INTERVAL = (process.env.INTERVAL * 1000);
async function run() {
console.log("Running Test!!!".green);
await snoolicious.getMentions(2);
console.log("Size of the queue: ", snoolicious.tasks.size());
await snoolicious.queryTasks(handleCommand, handleSubmission);
console.log(`Finished querying tasks. Sleeping for ${INTERVAL/1000} seconds...`.rainbow);
setTimeout(() => {
return run()
}, (INTERVAL));
}
(async () => {
await run();
})();
The output:
Preparing new database...
SELECT count(*) FROM sqlite_master WHERE type='table' AND name='saved';
Preparing statements...
Running Test!!!
MentionBot --Assigning hte FIRST utc...
Size of the queue: 2
Snoolicious Querying Tasks!
Calling back with handleCommand(task)
Bot -- handling a command! { directive: 'positive', args: [] }
Test passed
getting the parent submission...
Calling back with handleCommand(task)
Bot -- handling a command! { directive: 'positive', args: [] }
Test passed
getting the parent submission...
Finished querying tasks. Sleeping for 30 seconds...
Got this parent: Comment {...
Handling commands and getting parent submission: (snoolicious.requester = snoowrap.requester)
async function handleCommand(task) {
let id = `${task.item.parent_id}${task.item.created_utc}${task.item.id}`;
const checkedId = await db.checkID(id);
if (task.item.subreddit.display_name === process.env.MASTER_SUB) {
try {
validateCommand(task.command);
const parent = await getParentSubmission(task.item);
console.log("Got this parent: ", parent);
console.log("Checking against this item: ", task.item);
await checkUserRatingSelf(task.item);
await checkTypePrefix(task.item);
} catch (err) {
await replyWithError(err.message);
}
} else {
console.log("id HAS been seen: id ", checkedId);
}
}
// Get a parent submission:
async function getParentSubmission(item) {
console.log("getting the parent submission...".magenta);
if (item.parent_id.startsWith('t3_')) {
const rep = item.parent_id.replace('t3_', '');
const parent = await snoolicious.requester.getSubmission(rep);
return parent;
} else if (item.parent_id.startsWith('t1_')) {
const rep = item.parent_id.replace('t1_', '');
const parent = await snoolicious.requester.getComment(rep);
return parent;
}
}
解决方案
推荐阅读
- android - 在 Redmi Note7 的 camera2 API Android 中捕获图像时相机冻结
- apache-spark - 如何使用流数据帧进行 rdd 转换
- php - 如何从 Google API PHP 客户端获取 SMTP 凭据
- excel - 什么循环条件可以让代码连续运行
- android - 在编译 Kotlin 代码之前从 gradle 运行 CMake
- reactjs - 解析错误:意外的令牌,应为“{”
- c - GCC 和 Clang 之间的执行性能差距
- angular - 访问 ngOnChanges 中的最新输入属性
- php - 在生产环境中为管理员使用 Laravel Debugbar
- laravel - Laravel 从列或外键过滤