0

I'm having this piece of code written in javascript

preDefineListName = ['Applied', 'Test taken', 'SWS Interview', 'Candidate', 'Rejected'];

for (var i = 0; i < preDefineListName.length; i++) {
      Trello.addList(data.id, preDefineListName[i]);
};

Trello.addList = function (trelloBoardId, listName) {
    return $http.post('https://api.trello.com/1/lists', {
        idBoard: trelloBoardId,
        name: listName,
        key: trelloKey,
        token: trelloToken
    });
};

now above function Trello.addList in the for loop makes a list on the trello.com with the given names in preDefineListName. The problem is the lists are not appearing in the order as they passed.

What should I do to make it in proper order. and i've to call function in the loop so I can't change it.

9
  • 2
    Is the Trello.addList() method asynchronous? If so there's no way to guarantee the order the requests are returned in. You would need to hook an event to when all the requests are finished and then order the list. Commented Sep 16, 2015 at 7:03
  • What is data variable? Commented Sep 16, 2015 at 7:03
  • @RoryMcCrossan so is their any way that i can make Trello.addList() synchronous. Commented Sep 16, 2015 at 7:05
  • I don't know the Trello SDK, but unless they specifically have a method or option which is synchronous, then I doubt it. Commented Sep 16, 2015 at 7:06
  • @RoryMcCrossan Trello.addList() is not from Trello SDK I've made this method. Commented Sep 16, 2015 at 7:07

2 Answers 2

1

Your Trello.addList returns a Promise and is asynchronous (as it executes an http call). You therefore need an asynchronous loop instead of the for loop as well. This would be a .forEach call on the preDefineListName list.

You can however use .map as well, which lets you return the result of the Trello.addList calls and then use $q.all to wait until all addList calls are done:

$q.all(preDefineListName.map(function(name) {
    return Trello.addList(data.id, name);
})).then(function success(results) {
    // do something with the results
}, function error(reasons) {
    // handle errors here
});
Sign up to request clarification or add additional context in comments.

Comments

0

Use promises and recursion. Looks a bit hacky, but will make things synchronous:

preDefineListName = ['Applied', 'Test taken', 'SWS Interview', 'Candidate', 'Rejected'];

Trello.addList(data.id, preDefinedListName); // Initiate list adding

Trello.addList = function(trelloBoardId, listNames) {
   if(!listNames.length) {
      return;
   }

   var listName = listNames[0];
   listNames.splice(0, 1); // Remove first element from array

   $http.post('https://api.trello.com/1/lists', {
        idBoard: trelloBoardId,
        name: listName,
        key: trelloKey,
        token: trelloToken
   }).then(function(response) {
       Trello.addList(trelloBoardId, listNames); // Call the function again after this request has finished.
   });
}

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.