fix(@embark/embarkjs): Fix potential race condition

`EmbarkJS.Blockchain.connectConsole` has a potential race condition with both `cb` and `doneCb` firing at the end of the method.

This PR is an attempt to fix that by first awaiting the `cb`, then finally calling `doneCb`, as suggested by @michaelsbradleyjr in https://github.com/embark-framework/embark/pull/1319#discussion_r256850820
This commit is contained in:
emizzle 2019-02-15 07:54:44 +11:00 committed by Iuri Matias
parent 9ccc453379
commit 876eee5354
1 changed files with 9 additions and 3 deletions

View File

@ -49,12 +49,18 @@ Blockchain.connect = function(options, callback) {
Blockchain.connectConsole = function(doneCb) { Blockchain.connectConsole = function(doneCb) {
this.doFirst((cb) => { this.doFirst((cb) => {
this.blockchainConnector.getAccounts((err, accounts) => { this.blockchainConnector.getAccounts(async (err, accounts) => {
if (accounts) { if (accounts) {
this.blockchainConnector.setDefaultAccount(accounts[0]); this.blockchainConnector.setDefaultAccount(accounts[0]);
} }
cb(err); let _err = err;
doneCb(err); try {
await cb(_err);
} catch (e) {
_err = e;
} finally {
doneCb(_err);
}
}); });
}); });
}; };