0

I'm building an ember app, and I keep running into the same problem where I make a call to the store, but the function keeps compiling before the store has retrieved the data from the backend. Specifically I'm having the problem with a findRecord. I've implemented it both ways:

var admin = this.store.findRecord('admin', 1);
console.log(admin.get('season'));
console.log('Are we here?');

and

this.store.findRecord('admin', 1).then(function(admin) {
  console.log(admin.get('season'));
});
console.log('Are we here?');

In both cases, the Are we here? is logged BEFORE the season. Obviously the console logs are just for the example, and it creates an actual problem with what I'm trying to get done. Does anybody know a simple fix for this delay?

Thanks.

Tom
  • 350
  • 4
  • 21
  • 1
    Possible duplicate of [How do I return the response from an asynchronous call?](http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Alexei Levenkov Sep 03 '16 at 00:04
  • Note that [How do I return the response from an asynchronous call](http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) duplicate applies even if you don't use return value - you still expect something to happen after return from asynchronous function. – Alexei Levenkov Sep 03 '16 at 00:06
  • 1
    Some of Ember Data's APIs are **synchronous**, meaning the return with the value immediately. For instance, `peekRecord`. `findRecord` is **asynchronous**, meaning it returns a promise, and any processing must be done within a `then` clause. –  Sep 03 '16 at 05:03

2 Answers2

1

Of course it is. It's an asynchronous behavior. It takes some time to solve promise which is returned from findRecord(), thus the consequence is:

this.store.findRecord(); //synchronous
console.log('we are here'); //synchronous

in the meantime a promise returned from findRecord() gets resolved (asynchronous behavior)

console.log(admin.get('season'));
Lux
  • 17,835
  • 5
  • 43
  • 73
0

An asynchronous call will not stop your code from progressing, that´s the purpose of it. Else it would block UI updates and user interaction while loading data.