Note that there are some explanatory texts on larger screens.

plurals
  1. POCalling a closure inside a promise callback
    primarykey
    data
    text
    <p>I'm implementing queue system for a webapp so that HTTP request that are failed are locally stored to be re-executed later on.</p> <p>I have read Mozilla's documentation about <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Closures#Creating_closures_in_loops.3A_A_common_mistake" rel="nofollow" title="closure in loops">closure in loops</a> and then created inner closures.</p> <p>If running this with ids <code>[1,2,3]</code>, the output will be:</p> <pre><code>makeRequestRecovery 1 makeRequestRecovery 2 makeRequestRecovery 3 failure recover id 1 failure recover id 1 failure recover id 1 </code></pre> <p>code:</p> <pre><code>var recoverRequest = function(entry) { console.log('recover id', entry.data.id); $scope.storage.entries.push(entry); }; var makeRequestRecovery = function(entry) { console.log('makeRequestRecovery', entry.data.id); return function() { recoverRequest(entry); } }; $scope.syncEntries = function() { var initialLength = $scope.storage.entries.length; var requestCount = 0; while ($scope.storage.entries.length &gt; 0) { var entry = $scope.storage.entries.pop(0); var recover = makeRequestRecovery(entry); // restangular stuff... // restangular HTTP REMOVE entry.remove().then(function(data) { console.log('success!', data); }, function(data) { console.log('failure', data); recover(); }); requestCount++; if (requestCount &gt; 2 * initialLength) { // too many requests failed break; } } } </code></pre> <p>What changes should I make so <code>recover()</code> is executed with the right value?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload