1

Hi there I've been trying to use promise technique to wait for the execution of calls inside a for loop.However I am not able to hold on.

"return mainArr" gives undefined

//export CSV

Loop

//Assumes data is a json object list.

function getCustomData(data){

            var mainArr=[];
            for (var i = 0; i < data.length; i++) {
                var obj={};
                obj.word = $('#type-ahead-input').val();
                obj.synonym = data[i].word;
                obj.similarityCount = (data[i].similarityCount).toFixed(2);
                obj.emailCount = data[i].occuranceCount;
                obj.synonymlist = getSynonymList(data[i].word);
                mainArr.push(obj);

            }
            return mainArr;
        }
        function getSynonymList(inputWord){
            return WordService.getSynonymList({
                ids : inputWord
            }).$promise
                    .then(function($response) {
                        var output = $response;

                        var wordList =[];
                        for (var i = 0; i < output.length; i++) {
                            wordList.push(output[i].word);
                        }

                        return wordList;
                    });
        }

Factory Resource Call Signature

getSynonymList : {

        method : 'GET',
        isArray: true,
        async  : false,
        url : appRoot + '/synonym',
        params : {
            word : '@word'
        }
    },
2
  • 1
    Return value of getCustomData can't be undefined. Commented Jan 22, 2016 at 18:31
  • The return from an onFulfilled callback in the $promise.then method results in a $q promise, not data. Using the same name, getSynonymList, for a service that returns a $resource object, and a function that returns a promise makes your code confusing and difficult to understand. Commented Jan 22, 2016 at 19:11

1 Answer 1

1

You need to push the value into an promises array, and use $q.all() to resolve multiple promises

function getCustomData(data){

        var promises = []; // an array of promises
        for (var i = 0; i < data.length; i++) {
             // rest of you code here

            var promise = $q.when(data);  // returns a promise             
            promises.push(obj);
        }

        return $q.all(promises);
    }

then you can call the return values using callback

myservice.getCustomerData(data).then(
  function successcallback(returnvalue){
    console.log(returnvalue);
  }, 
  function errorcalback(error){
    console.log(returnvalue);
}

This applies to both your functions, since you are using loops. (Note that I have not implemented the above code, but should provide you with an outline)

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

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.