paldepind/synceddb

Variable not available out of function scope

micheldejoode opened this issue · 8 comments

Hi Simon,

I noticed that variables (or changes in the value of variables) within a function (for example the "get" function) are not available outside of that function, even if I declare this variable first outside of this function (to make it global).

I don't know if this is related, but I noticed also that I cannot use "return" to return a variable after I called a function.

I wonder if this is a bug, or if I'm doing something wrong.

If you do something like this:

var koe;
db.ranges.byCreation.get(datum).then(function(ranges) {
  ranges.forEach(function(range) {
    koe = range.description;
  });
  return koe;
});

Then koe should be updated outside of the closure. But the get here is asynchronous. So koe is only set after a certain amount of time. Likewise the returned koe on the second last line should be returned properly. But inside a promise.

Does that help you? Or am I misunderstanding your question?

Hi Simon, that is indeed what I want, but somehow it doesn't work with me.
I haven't used the "db.syncContinuously('tasks');" in my code though, could
that be the reason why it doesn't work with me? I will test that out today.

Greetings,
Michel

Op ma 28 sep. 2015 om 15:13 schreef Simon Friis Vindum <
notifications@github.com>:

If you do something like this:

var koe;
db.ranges.byCreation.get(datum).then(function(ranges) {
ranges.forEach(function(range) {
koe = range.description;
});
return koe;
});

Then koe should be updated outside of the closure. But the get here is
asynchronous. So koe is only set after a certain amount of time. Likewise
the returned koe on the second last line should be returned properly. But
inside a promise.

Does that help you? Or am I misunderstanding your question?


Reply to this email directly or view it on GitHub
#4 (comment).

Could you maybe post a short example that shows exactly what does not work as expected?

Hi Simon,

I found out it has something to do with the fact that the database function
is in the "promise". The variable I want to set is still empty if I use it,
because the database (for example "get") is still busy getting a value. So
it's not a bug. I have to find out how to do the things I want in
combination with the "promise". Is there some function like "wait until
promise is done" or something?

Greetings,
Michel

Op ma 28 sep. 2015 om 15:42 schreef Simon Friis Vindum <
notifications@github.com>:

Could you maybe post a short example that shows exactly what does not work
as expected?


Reply to this email directly or view it on GitHub
#4 (comment).

Is there some function like "wait until promise is done" or something?

Yes, indeed. That is the then method.

db.ranges.byCreation.get(datum).then(function(ranges) {
  ranges.forEach(function(range) {
    koe = range.description;
  });
  return koe;
}).then(function(koe) {
  // Here we have `koe`
});

Great, I think that is what I need! I will try it out, and let you know how
it works out. I will update the issue tracker also :-)

Greetings,
Michel

Op ma 28 sep. 2015 om 21:52 schreef Simon Friis Vindum <
notifications@github.com>:

Is there some function like "wait until promise is done" or something?

Yes, indeed. That is the then method.

db.ranges.byCreation.get(datum).then(function(ranges) {
ranges.forEach(function(range) {
koe = range.description;
});
return

koe;
}).then(function(koe) {
// Here we have koe
});


Reply to this email directly or view it on GitHub
#4 (comment).

This issue seems able to be closed.

You are correct :)