Apologies if I am missing something trivial. What I want to do is to call database queries synchronously. I need to make sure the database inserts are completed before moving to the next step. I also do not want to nest in the callback. I am trying to use promises but it doesn't seem to work as I expected. This is my code:
async init()
{
await this.initWalletTable();
}
async initWalletTable()
{
console.log("Creating wallet table");
var self = this;
walletParams.wallets.forEach(function(wallet) {
(async() => {
await self.insertWallet(wallet);
console.log("inserted " + wallet.multisig_wallet_name);
})();
});
}
insertWallet(wallet)
{
console.log("Inserting wallet " + wallet.multisig_wallet_name);
return new Promise((resolve, reject) => {
pool.query(`INSERT INTO ${schema}wallet \
(name, wallet_name_1, ) \
VALUES ($1, $2) \
ON CONFLICT (name) DO NOTHING`, [wallet.multisig_wallet_name, wallet.wallet_name1])
.then(dbres => {
return resolve(true);
})
.catch(e => {
console.error(e.stack);
return resolve(true);
})
});
}