knex承诺 - 如何在两个功能完成他们的工作时等待

时间:2018-01-13 17:20:47

标签: javascript node.js knex.js

如何添加仅在两个进程完成后才能运行的代码?

normalise1();
normalise2();

function normalise1() {
    return knex("ingredients")
        .select("name", "id")
        .map(function (ingredient) {            
            var normalised_name = common.normalise(ingredient.name);
            knex('ingredients').where('id', ingredient.id).update({ name_normalised: normalised_name }).then();
        });
};

function normalise2() {
    return knex("synonyms")
        .select("synon_name as name", "id")
        .map(function (ingredient) {            
            var normalised_name = common.normalise(ingredient.name);
            knex('synonyms').where('id', ingredient.id).update({ synon_name_normalised: normalised_name }).then();
        });
};

我尝试过不同的方式

Promise.all([normalise1(), normalise2()])
    .then(() => console.log('Done'));

但它不起作用。 基本上,console.log('Done')在所有进程完成之前出现。我相信这是因为缺少Promise部分内部函数,但我无法确切地知道如何。

1 个答案:

答案 0 :(得分:2)

传递给Promise.all()时不会调用这些函数,Promise不返回.map()

return来电调用这些功能和knex() .map(),这可能还需要在函数调用中使用Promise.all()