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...

5
  • items.length will be zero if there is nothing in the array. Commented Nov 3, 2016 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? Commented Nov 3, 2016 at 20:38
  • Make it recursive by calling offer.getReceivedItems again in the else. Or, better yet, use Promises with resolve and reject. Commented Nov 3, 2016 at 20:39
  • @ScottMarcus can you show me an example? i am not so good in javascript Commented Nov 3, 2016 at 20:40
  • See multiple, sequential fetch() Promise . When should process conclude? Commented Nov 3, 2016 at 20:41

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

4 Comments

I know that with the checking of length, but how i can restart from beginning, if the length is zero?... that is my problem :/
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); } ... }
that i know... but how i can get back to the start?... @teroi that he trys again
What do you mean by "back to the start"?
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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.