Replace forEach() with _asyncEach() to block execution until loop callbacks complete.

This commit is contained in:
Andy Phillipson 2017-01-25 11:09:26 -05:00
commit dbdd6ad6bd

View file

@ -176,6 +176,7 @@ angular.module('copayApp.services')
});
});
}
callback();
}, function() {
// done
// Remove obsolete key.
@ -237,7 +238,7 @@ angular.module('copayApp.services')
'the following accounts validated OK: ' + (verified.length > 0 ? verified : 'none'));
});
} else {
cb(null, (verified.length > 0 ? 'accounts OK: ' + verified : ''));
cb(null, (verified.length > 0 ? 'accounts OK: ' + verified : 'no accounts found'));
}
});
});
@ -332,8 +333,8 @@ angular.module('copayApp.services')
var errorMessage = undefined;
var keys = Object.keys(_upgraders).sort();
var networks = ['livenet', 'testnet'];
_asyncEach(keys, function(key, callback) {
networks.forEach(function(network) {
_asyncEach(keys, function(key, callback_keys) {
_asyncEach(networks, function(network, callback_networks) {
var storagekey = key.split('_')[1];
_upgraders[key](storagekey, network, function(err, msg) {
if (err) {
@ -342,11 +343,14 @@ angular.module('copayApp.services')
errorMessage = errorCount + ' storage upgrade failures';
}
if (msg) _handleUpgradeSuccess(storagekey + '-' + network, msg);
callback();
callback_networks();
});
}, function() {
// done - networks
callback_keys();
});
}, function() {
//done
//done - keys
cb(errorMessage);
});
};
@ -697,15 +701,18 @@ angular.module('copayApp.services')
}
bitpayAccounts = bitpayAccounts || {};
var cards = [];
Object.keys(bitpayAccounts).forEach(function(email) {
_asyncEach(Object.keys(bitpayAccounts), function(email, callback) {
// For the UI, add the account email to the card object.
var acctCards = bitpayAccounts[email]['bitpayDebitCards-' + network] || [];
for (var i = 0; i < acctCards.length; i++) {
acctCards[i].email = email;
}
cards = cards.concat(acctCards);
callback();
}, function() {
// done
cb(err, cards);
});
cb(err, cards);
});
};
@ -727,7 +734,7 @@ angular.module('copayApp.services')
bitpayAccounts = JSON.parse(bitpayAccounts);
}
bitpayAccounts = bitpayAccounts || {};
Object.keys(bitpayAccounts).forEach(function(email) {
_asyncEach(Object.keys(bitpayAccounts), function(email, callback) {
var data = bitpayAccounts[email]['bitpayDebitCards-' + network];
var newCards = lodash.reject(data, {
'eid': card.eid
@ -741,12 +748,15 @@ angular.module('copayApp.services')
root.getBitpayDebitCards(network, function(err, cards) {
if (err) cb(err);
if (cards.length == 0) {
root.removeNextStep('BitpayCard', cb);
root.removeNextStep('BitpayCard', callback());
} else {
cb();
callback()
}
});
});
}, function() {
// done
cb();
});
});
};