From dbdd6ad6bd3cbd088252e8ba109e9f286bb70d71 Mon Sep 17 00:00:00 2001 From: Andy Phillipson Date: Wed, 25 Jan 2017 11:09:26 -0500 Subject: [PATCH] Replace forEach() with _asyncEach() to block execution until loop callbacks complete. --- src/js/services/storageService.js | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/src/js/services/storageService.js b/src/js/services/storageService.js index a9ebe98e2..ebd2b53d7 100644 --- a/src/js/services/storageService.js +++ b/src/js/services/storageService.js @@ -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(); }); }); };