Q:

Run an event only once in NodeJS but use the data multiple times

So I have written this code : 
     manager.getInventoryContents(730, 2, true, (err,inventory,currencies) => {
  if (err){
    console.log(err)
  } else {
    console.log("Create order for : " + orderitemname.length + " items.")
    var otn = orderitemname.length -1;
    while (otn !== -1) {
      var li= inventory.length - 1;
      while (li !== -1){
        if (inventory[li].market_name === orderitemname[otn]){
          console.log("Add item to trade " + orderitemname[otn]);
          li = li -1;
          otn = otn -1;
        } else {
          console.log("ERR !!! ITEM NOT IN INVENTORY !!! " + orderitemname[otn]);
          //Change Order Status To Failed !!
          n = n-1;
          otn = -1;
          li = -1;
        }
      }
    }
  }
})

So what is happening is that the process is calling this event multiple times (since it is in a while loop), and the node module is ignoreing it saying : 
Error: The request is a duplicate and the action has already occurred in the past, ignored this time

So is there a way I can just call the event once, save it to a variable or something and then use the data multiple time ?

A:

You can wrap everything in a new context and initiate whatever variable you need there. Then, the variable will be accessible from within your callback function.
Something like this:
(() => {
    let ctx_data = 0;
    manager.getInventoryContents(730, 2, true, (err,inventory,currencies) => {
        console.log(++ctx_data);
    });
})();

