Promise.all不是在等待诺言吗?

时间:2020-10-22 00:00:36

标签: javascript node.js asynchronous promise

此函数需要2个异步回调。我不知道为什么,但是当调用这些回调时,它们等待的承诺没有被正确等待。我认为这可能与我给他们打电话的方式有关。我对Promise API不太熟悉,所以我只是一起破解了。如果有人可以告诉我我做错了什么,我将不胜感激。

   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);
    }

或者也许是我称之为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();
    })();

输出:

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 {...

处理命令并获取父提交:(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;
    }
}

0 个答案:

没有答案