0
var deferred = $.Deferred();

function doSomething() {
    for (i = 1; i < 10; i++) {
        context.executeQueryAsync(function () //success function
        {
            if (check_i_val(i)) {
                context.executeQueryAsync(Function.createDelegate(this, this.success), Function.createDelegate(this, this.failed));
            }
        },

        function (sender, args) //failure function
        {
            console.log(args.get_message());
        });
    }
}

function success() {
    console.log(i);
}

function failed() {
    //display error
}

function check_i_val(val) {
    if (val = 1 || val = 3 || val = 5 || val = 7) {
        return true;
    }
}

I want this to produce 1,3,5,7 in that order - the actual function i'm using is more complex than this and has loading times so the results are unexpected. How can I use deferred (as a jquery promise) so that my code will run sequentially?

John
  • 147
  • 3
  • 11
  • You realize that your `check_i_val` function is wrong, right? – Benjamin Gruenbaum Oct 23 '14 at 18:38
  • Also, that there isn't anything relating to promises besides your empty deferred in your code. Maybe you want to start with something smaller? – Benjamin Gruenbaum Oct 23 '14 at 18:39
  • ok fixed the function, i just created it as an example, been programming in too many languages lately. I know this is a complicated problem but it's the problem I am facing. I need to make sure that the loop only iterates once it receives a promise from the inner async function – John Oct 23 '14 at 18:56
  • Where does `executeQueryAsync` come from and [have you tried promisifying it](http://stackoverflow.com/questions/22519784/how-do-i-convert-an-existing-callback-api-to-promises) ? – Benjamin Gruenbaum Oct 23 '14 at 18:57
  • It's part of sharepoints javascript API http://msdn.microsoft.com/en-us/library/office/dn168907%28v=office.15%29.aspx – John Oct 23 '14 at 18:59
  • 2
    Ok - you need to promisify it (the linked thread from my earlier comment explains how) and then use `then` chaining in order to have it run in sequence. You can read more about `then` chaining if you search for questions about sequential execution with promises in StackOverflow - basically it'd entail doing `p = p.then(code for next function - with return of async)` in a loop. – Benjamin Gruenbaum Oct 23 '14 at 19:02
  • Thanks for the help. I am going to continue learning based off of your recommendation. – John Oct 24 '14 at 14:30
  • Right, note the answe you accepted does something different from what you wanted. – Benjamin Gruenbaum Oct 24 '14 at 15:31

1 Answers1

1
function mockExecuteQueryAsync(inValue) {
    var outValue = $.Deferred();

    window.setTimeout(function() {
        window.console.debug(Date.now(), 'Resolving ', inValue);
        // Let's just return the value we got as input
        outValue.resolve(inValue);
    }, Math.random() * 10000); // Random interval...

    return outValue.promise();
}

var arrayOfPromises = [];
var arrayOfValues = [1, 3, 5, 7];
for (var i in arrayOfValues) {
    arrayOfPromises.push(mockExecuteQueryAsync(arrayOfValues[i]));
}
$.when.apply($, arrayOfPromises).done(function() {
    window.console.debug(Date.now(), 'And now we have all the results...');
    for (var i in arguments) {
        window.console.log(arguments[i]);
    }
});
Markku Uttula
  • 126
  • 1
  • 3