1

Hi I believe I need to execute the following code in sequence otherwise if lists get long I may get errors because one async call may affect the other. I have two cascaded async calls. I have only ran the first lockListItems function but dubious about the reliability of running the remaining 3 functions. Currently when executed I get the following console message order : end main, List Item Loaded, end 2nd success.

Would be better to execute the next lockListItems function after console message 'end 2nd success' or does it not matter?

Is there an easy way to achieve, Promises could work but is it an overkill?

UPDATE: calling single function 'lockListItems' works but if I try to run the 2nd function after the 1st an error is reported. 'Collection not initialised'

lockListItems (ID,List1Name)
lockListItems (ID,List2Name)
lockListItems (ID,List3Name)
lockListItems (ID,List4Name)
console.log ('end main');


function lockListItems(tID,varList) {
//load items from varList where ...
executeAsync (success lockListItems, fail lockListItems)
console.log('List Item Loaded');
}

function onQuerySucceeded lockListItems(sender, args)
{
//Add required items ID to Array
UpdateStatusLocked(ArrItemsUpdate);

}

function UpdateStatusLocked(ArrItemsUpdate) {
//loop array and upade
executeAsync (success UpdateStatusLocked, fail UpdateStatusLocked
}

function onQuerySucceededUpdateStatusLocked(sender, args) {
//alert ('Updated Locked Status');
console.log('end 2nd success- List Item Completed');
}

2 Answers 2

1

A recursive function could be easier

pseudo code:

var update=[1,2,3,4,5];

function lockitems(){
    if(update.length>0){
        var lockone=update.pop();
        executeAsync(success);
    }
}
function success(){
    lockitems();
}
0

First one comment:

function lockListItems(tID,varList) {
    //load items from varList where ...
    executeAsync (success lockListItems, fail lockListItems)
    console.log('List Item Loaded'); // <-- your list isn't loaded here. The function is finished, thats all.
}

As far is I know this is how you would solve this:

function firstList() {
    //your query and load calls here
    executeQueryAsync(firstListSuccess, onError);
}

function firstListSuccess() {
    console.log("first list loaded");
    //your query and load calls here
    executeQueryAsync(secondListSuccess, onError);
}

function secondListSuccess() {
    console.log("second list loaded");
    //your query and load calls here
    executeQueryAsync(thirdListSuccess, onError);
}

//etc

function onError() {
    consol.log('error');
}
2
  • Hi and thanks - I guess this method should work, but the functions need to be written every time. There should be a solution to make it re-useable? Commented Jan 29, 2016 at 15:13
  • This is just to make the sequential calls. You could put your logic into a separate function. Commented Jan 31, 2016 at 13:09

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.