3

I need to execute 2 functions one after the other with the stuff in function "A" fully completing before the stuff in function "B" executes...

I can't find an example that is not using setTimeout .. which is strange ...

I have the following example below ( from here ) , is it supposed to work ?? How could I test if it is working ? What dummy code could I use to simulate the part "//do stuff with SharePoint JSOM" taking 5 secs to 30 secs ,say .

var a = function() {
var defer = $.Deferred();

//do stuff with SharePoint JSOM

console.log('a() called');

return defer;
};

var b = function() {
var defer = $.Deferred();

console.log('b() called');


return defer;
};



a().then(b);
Community
  • 1
  • 1
Zertix.net
  • 55
  • 7
  • Yes, just use `setTimeout` as a dummy. – Bergi Aug 23 '16 at 12:20
  • If `b` doesn't do anything asynchronous, it should not return a deferred (or promise). `then` also works with synchronous callbacks. – Bergi Aug 23 '16 at 12:20
  • according to what I tested and read you can't use setTimeout as a dummy since it simply does not halt the calling code. And I can't use preset delays. – Zertix.net Aug 23 '16 at 12:24
  • Yes, deferreds or JSOM don't halt the calling code either, that's their whole point. – Bergi Aug 23 '16 at 12:24
  • HI I don't mean halt the code completely. Just halt the execution of function B until function A executes, ( without using setTimeout ), Are you saying this is not the intended purpose of Jquery deferred/promises ? – Zertix.net Aug 23 '16 at 12:28
  • `b` is not halted - it doesn't execute yet. It's deferred, scheduled to be executed after `a` finishes. *That's* what deferred objects do. So yes, just use `setTimeout` to asynchronously resolve the promise that `a` returns. – Bergi Aug 23 '16 at 12:34

1 Answers1

4

Simple use a promise (vanilla JS) and chain them with then.

function a() {
    return new Promise(function(resolve) {
        console.log("wait two seconds ...");

        // this timeout is only here for demo purpose ;)
        setTimeout(function() {
            console.log("A");
            resolve();
        }, 2000);
    });
}

function b() {
    console.log("B");
}

a().then(b);

If you want to use jQuery deffered its nearly the same.

function a() {
    var defer = $.Deferred();
    
    console.log("wait two seconds ...");

    // this timeout is only here for demo purpose ;)
    setTimeout(function() {
        console.log("A");
        defer.resolve();
    }, 2000);
    
    return defer;
}

function b() {
    console.log("B");
}

a().then(b);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
eisbehr
  • 12,243
  • 7
  • 38
  • 63
  • Hi, I am aware of this way, But was just wondering to get a working example using jquery promises... – Zertix.net Aug 23 '16 at 12:27
  • But doesn't setTimeout fix a set timeout of 2000 ms ? I don't want a fixed timeout. I want B to execute whenever A finishes naturally. – Zertix.net Aug 23 '16 at 14:00
  • 1
    `setTimeout` is only for example there. You can completly remove it. ;) See here: https://jsfiddle.net/cxcrk8vr/ @Zertix.net – eisbehr Aug 23 '16 at 14:01
  • OK , so you are saying with defer.resolve(); outside the setTimeout statement, it would still wait for all stuff ( functions,etc) to complete as long as defer.resolve(); is the last line ?. I couldn't test it since I haven't got some dummy function that runs for seconds on its own.. – Zertix.net Aug 23 '16 at 20:03
  • Correct. Calling `defer.resolve();` says `ok, I'm done, next one please`. ;) For example, you could call `defer.resolve();` in a ajax callback. So the execution of `b()` would wait until the ajax request is complete. @Zertix.net – eisbehr Aug 23 '16 at 20:06
  • 1
    @Zertix.net well, no, because you would still want to execute .resolve() only after whatever action you are performing completes, which will require some kind of callback (just like setTimeout does). promises don't avoid the need for callbacks. – Kevin B Aug 23 '16 at 20:19
  • I think he asks because he want to know if `setTimeout` is **needed** for `deffered` or `promise`. And it is not. He can call `resolve` whenever the action of `a()` is finished. If in an `setTimeout`, `ajax` or anything else. – eisbehr Aug 23 '16 at 20:26