15

I need to call a fetch with a synchronous call, I know with jquery ajax I can use {async: false} can I pass this option to fetch function ?

Thaddeus Albers
  • 4,094
  • 5
  • 32
  • 42
Petran
  • 7,677
  • 22
  • 65
  • 104
  • possible duplicate of [Wait for the collection to fetch everything in backbone](http://stackoverflow.com/questions/10491230/wait-for-the-collection-to-fetch-everything-in-backbone) – Nick Tomlin Aug 26 '13 at 16:50
  • 2
    A better question is why you need it to be synchronous. And the likely answer is that you're doing something the wrong way. – idbehold Aug 26 '13 at 18:27
  • It's an edge case scenario yes. – Petran Aug 26 '13 at 18:31
  • 1
    Note that solution, suggested here, doesn't work anymore, since recent Mozilla Firefoxes don't allow async in jquery.ajax. – Boris Burkov Oct 29 '14 at 11:30

2 Answers2

32

So the short answer is yes, you can simple call fetch function with param

{async:false}.
Roy M J
  • 6,926
  • 7
  • 51
  • 78
Petran
  • 7,677
  • 22
  • 65
  • 104
14

Actually backbone fetch method internally calls ajax. So you can pass any ajax options to backbone fetch method.

collection.fetch({
  // ajax options
   async: false, // by default it is true
   success: function(collection, response, options){
             console.log("success")
          },
   error: function(collection, response, options){
             console.log("error")            
          }
});
ni3
  • 485
  • 4
  • 20