Advertisement
Guest User

Untitled

a guest
May 29th, 2015
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.70 KB | None | 0 0
  1. //Question: if you have a function which may or may not do some async, do you use a promise for the non-async path?
  2.  
  3. //Option A: Only return promise when doing async:
  4. function OptionA(input){
  5. if ( requiresQuery() ){
  6. var defer = Q.deferred();
  7.  
  8. $.ajax()...
  9. .done(function(asyncData){
  10. defer.resolve(asyncData);
  11. });
  12. return defer.promise;
  13. }
  14.  
  15. return input * 2;
  16. }
  17.  
  18. //Option B: Always return a promise, resolve the non-async;
  19.  
  20. function OptionB(input){
  21. var defer = Q.deferred();
  22.  
  23. if ( requiresQuery() ){
  24. $.ajax()...
  25. .done(function(asyncData){
  26. defer.resolve(asyncData);
  27. });
  28.  
  29. }
  30. //Return a resolved promise
  31. defer.resolve(nonAsyncData);
  32. return defer.promise;
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement