如何添加仅在两个进程完成后才能运行的代码?
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部分内部函数,但我无法确切地知道如何。
答案 0 :(得分:2)
传递给Promise.all()
时不会调用这些函数,Promise
不返回.map()
。
从return
来电调用这些功能和knex()
.map()
,这可能还需要在函数调用中使用Promise.all()
。