title: How to get next and previous element of object in javascript?
body: <p>For a sample trading app i have an array, with trading pair symbols. And i want to fetch updates, but do it sequentially, eg:</p>
  
  <pre class="lang-js prettyprint-override"><code>var pairList = ['BTCUSD', 'ETHBTC', 'ETHUSD'];
  var currentPair = 0; // now pointing, that next time fetcher would fetch 'BTCUSD' pair
  var candlesData = [];
  
  update_current_pair = () {
    // for simplicity imagine that fetch io completes instantly, but it ALSO changes pairList
    candlesData[currentPair] = fetch_data_from_server_for_pair(currentPair);
  
    // pairList is changed, for example now 'BTCUSD' pair is removed.
    pairList = ['ETHBTC', 'ETHUSD'];
  
    // then i usually do:
    currentPair++; // increment
    currentPair %= pairList.length; // modulo
    // but OOPS! i just skipped 'ETHBTC' pair!
  
    setTimeout(update_current_pair, 1000);
  }
  
  // start fetcher
  update_current_pair();
  </code></pre>
  
  <p>How can i not skip 'ETHBTC' pair?</p>
  
  <hr>
  
  <p>Sample code: <a href="https://repl.it/@krieviedkoKtulk/jsasyncissue" rel="nofollow noreferrer">https://repl.it/@krieviedkoKtulk/jsasyncissue</a></p>
  
  <p>desired:</p>
  
  <pre><code>updating BTCUSD
  updated BTCUSD
  updating ETHBTC
  updated ETHBTC
  updating ETHUSD
  updated ETHUSD
  updating ETHBTC
  updated  ETHBTC
  </code></pre>
  
  <p>observed:</p>
  
  <pre><code>updating BTCUSD
  updated ETHBTC &lt;- data from BTCUSD is put to ETHBTC by an error
  updating ETHUSD &lt;- fetching ETHBTC is skipped, but proceed to next ETHUSD
  updated ETHUSD
  updating ETHBTC
  updated ETHBTC
  updating ETHUSD
  updated ETHUSD
  </code></pre>