Chain functions with deferred and promises but without setTimeout ...

Chain functions with deferred and promises but without setTimeout ...

Hi Guys

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 ... 


  1. var a = function() {
  2.     var defer = $.Deferred();
  3.       
  4. //do stuff with SharePoint JSOM

  5.     console.log('a() called');
  6.     defer.resolve();
  7.     return defer;
  8. };

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

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

  12.     defer.resolve();
  13.     return defer;
  14. };



  15. a().then(b);