-1

Promises are confusing. The below is obviously bad, incomplete and not how I would actually go about doing it but should be enough to clear up my questions.

Given the below code, is the if statement blocked until the User.find promise is resolved? or is it a race condition to the console.log at the end?

var user = null;

if (....) {
     User.find(123)
         .on("success", function(u) {
            user = u;
          });
}
else {
   // do something else to user
 }

 console.log(user.Name);

Likewise, if there is a promise being called within a promise is the outer promise(User.find) blocked until the inner one(Posts.findAll) is resolved? Again I know this is bad code, just trying to work out the flow and scope in my head.

var postlist = [];    
User.find(123)
     .on("success", function(user) {
       Posts.findAll({where:{user_id:user.id}})
            .on("success", function(posts) { postlist = posts});
     });
console.log(postlist);

Again, I am not looking for the "correct or best" way of doing this, just trying to get a grasp on the flow and scope. Sorry to keep reiterating it, but I've grown tired of people not answering the questions asked in loo of offering their advice -- bad experiences on other sites, hopefully that wont happen here.

Ryhnn
  • 456
  • 1
  • 5
  • 16
  • The problem you're having isn't promises, it's asynchronous code. I'm inclined to mark this as a dupe of http://stackoverflow.com/q/14220321/497418. – zzzzBov Apr 02 '15 at 13:37
  • @zzzzBov Thanks, that actually clears things up quite a bit. makes more questions though, like how to maintain state throughout a chain. – Ryhnn Apr 02 '15 at 13:45
  • This has very little to do with scope and a lot to do with time and how javascript code executes. I suggest researching the javascript event loop. here, i'll save you some time: http://www.youtube.com/watch?v=8aGhZQkoFbQ – Kevin B Apr 02 '15 at 15:05
  • @KevinB to me its a scope issue I'm trying to work out. For instance, in the second example. If there was a value in the postlist array, would the state of postlist be known inside the success method of the findAll as of the time it was called, or would it see it with any changes that had occurred, or something else? – Ryhnn Apr 02 '15 at 15:22
  • But it's not scope at all. If time weren't a factor, postlist would have infact updated properly. But, because the callback happens later, at a different time, it doesn't update the variable before the console.log happens. – Kevin B Apr 02 '15 at 15:26
  • The video i linked explains this very well. Think of mongodb (or whatever db you're using) as a 3rd party api that always runs asynchronously, very similar to setTimeout and setInterval – Kevin B Apr 02 '15 at 15:29
  • maybe its a terminology thing, the way I was taught its scope, if it was synchronous anyway. How would you maintain or pass the state of postlist through the async functions? – Ryhnn Apr 02 '15 at 15:30
  • Simply don't run any code outside of callbacks once you start using asynchronous methods. – Kevin B Apr 02 '15 at 15:30
  • what if you need the results from several async functions before you can run something else? there has to be someway of maintaining state or passing the data along. – Ryhnn Apr 02 '15 at 15:32
  • As a proof of concept, wrap your console.log in a setTimeout that delays for 10 seconds. it will then print what you expect it to (assuming the db work is done in 10 seconds or less) – Kevin B Apr 02 '15 at 15:32
  • You can do that using promise chains. but, first, you have to install a promise module – Kevin B Apr 02 '15 at 15:33
  • yes, but how. I haven't seen any examples anywhere which do this. from what I've experienced using .then() is variables declared in one aren't available in the next. – Ryhnn Apr 02 '15 at 15:33
  • You're not going to get any suggestions to external resources here for that. – Kevin B Apr 02 '15 at 15:34
  • You need a full on tutorial that explains what promises are and how promise chaining works, there's plenty of them all over the internet, but explaining that here in a single answer is a bit much (and has likely already been done) – Kevin B Apr 02 '15 at 15:36
  • Found one: http://stackoverflow.com/a/23833930/400654 – Kevin B Apr 02 '15 at 15:39
  • To be clear, you aren't using promises in your question. – Kevin B Apr 02 '15 at 15:41
  • found my answer here: http://stackoverflow.com/questions/25254355/how-to-pass-extra-data-down-a-parse-promise-chain – Ryhnn Apr 02 '15 at 15:51

1 Answers1

2

The promise does not block. Code execution continues concurrently.

Martin Krämer
  • 567
  • 3
  • 17