0

I have this function

offer.getReceivedItems(function(err, items) {

It returns an array (items) or throws an error err if it failed. Many times, when there isn't an err, the items array is empty. Like

[]

But when this array is empty, I need to try the same function again

offer.getReceivedItems(function(err, items) {

but how I can go back to it, when items is empty...

I tried so much, but I cannot find it...

Code looks like

offer.getReceivedItems(function(err, items) {
        if (err) {
            console.log("Couldn't get received items: " + err);
            offer.decline();
        } else {
            console.log(items);
            items.forEach(function(item,i,arr){
            ....

The forEach doesn't run when there is an empty array...

user2441511
  • 11,036
  • 3
  • 25
  • 50
Scholli
  • 419
  • 1
  • 6
  • 15
  • `items.length` will be zero if there is nothing in the array. – VLAZ Nov 03 '16 at 20:37
  • @vlaz that i know. But how i can "restart" that he trying this function again... i can make `if(items.length > 0)` but how i can make that he do offer.getReceivedItems again? – Scholli Nov 03 '16 at 20:38
  • Make it recursive by calling offer.getReceivedItems again in the else. Or, better yet, use Promises with resolve and reject. – Scott Marcus Nov 03 '16 at 20:39
  • @ScottMarcus can you show me an example? i am not so good in javascript – Scholli Nov 03 '16 at 20:40
  • See [multiple, sequential fetch() Promise](http://stackoverflow.com/questions/38034574/multiple-sequential-fetch-promise/38034756?s=7|1.0963#38034756) . When should process conclude? – guest271314 Nov 03 '16 at 20:41

2 Answers2

1

You should check if the array is empty before iterating with forEach. Check items.length.

There is a much longer, detailed explanation in this StackOverflow answer.

Community
  • 1
  • 1
user2441511
  • 11,036
  • 3
  • 25
  • 50
  • I know that with the checking of length, but how i can restart from beginning, if the length is zero?... that is my problem :/ – Scholli Nov 03 '16 at 20:42
  • What function is `offer.getReceivedItems` inside of? (E.g., if it's called `tryItems`, like so: `function tryItems(args) { offer.getRecievedItems ... }`) You can recursively call that external function inside the `else` of your code snippet, like was suggested by Scott Marcus in a comment above, e.g. `else { if (items.length === 0) { tryItems(args); } ... }` – user2441511 Nov 03 '16 at 20:51
  • that i know... but how i can get back to the start?... @teroi that he trys again – Scholli Nov 03 '16 at 20:51
  • What do you mean by "back to the start"? – teroi Nov 08 '16 at 15:23
1

I am having trouble understanding what it is you are trying to do but does the following help you?

var callBack = function(err, items) {
        if (err) {
            console.log("Couldn't get received items: " + err);
            offer.decline();
            offer.getReceivedItems(callBack); // Call again
        } else {
            console.log(items);
            items.forEach(function(item,i,arr){
            ....
        }
    };

// Original call
offer.getReceivedItems(callBack);
vbguyny
  • 1,170
  • 1
  • 10
  • 29