Mathematical operations on the contents of a variable I've created by looking up a LocalStorage key?

Hi, whatever I seem to do, I can’t seem to be able to perform mathematical operations (or boolean operations, or any other kind) on data I put into a variable, when I get this data from LocalStorage. I can display the data fine, so I know it’s there, but when I try to use it in an if/do, or a mathematical expression, it returns NaN or null.

Please help! Example below.


Operations on the value obtained from the LocalStorage must be performed in the “then do” part. Blocks with the “then do” part are asynchronous blocks.

1 Like


answer from actech in blocks :slight_smile:

1 Like

Is there no way to assign the value obtained to a new variable and perform operations on that variable later on?

This can be done. But assign the value of the new variable to the inside of the function “do something”.

Mobile programming differs from desktop programming in that it uses the concept of asynchronous programming. Look at the difference between the synchronous function call from asynchronous to better understand this.

Here’s an example showing how:

Hmmm… The problem is I don’t want to use this procedure to display text. I just want the procedure to go get the value from LocalStorage and put it in a (numerical) variable, then come back.

See, I need to get a bunch of these values from different places, perform operations on them in different procedures, and then display the answers separately, later on.

The whole reason I’m doing this is because it seems like using local storage is the only way to do global variables. Basically that is what I want- a workaround for the fact that there are no global variables in thunkable x.

It seems like I’m missing something. Is the only way to do this really to nest the entire mathematical operation in the then do, every time?? That seems really inelegant, but maybe that’s the only solution here?

You are right that it is inelegant! It’s hard to make asynchronous operations like 'Local_Storage1 call Get' be elegant and easy for users to understand. We are working on some things that ought to help the situation (like variables that are global to the entire app). Unfortunately I don’t have an ETA yet on when that will be available.


1 Like

OK. So I guess there’s no way to do it.

It’s super weird to me that we can get the contents of LocalStorage into a textbox for display, but not actually get them into a variable for performing math or logic on.

No workaround at all?

The way to do it is to ensure that all the work that you want to do with the value that you get from LocalStorage is done within the ‘then do’ slot of the ‘Local_Storage1 call Get’ block.

One way to think about these ‘asynchronous’ blocks is that blocks like ‘Local_Storage1 call Get’ are performed in parallel (i.e. at the same time) as the blocks that come after them. There is no guarantee that the asynchronous block will be done with its work before those blocks which come after them. That is why accessing the enterednumber variable after your call to the do something block doesn’t work - the do something block hasn’t yet set the enterednumber variable. The ‘then do’ slot is there specifically to provide a way to do further work after the ‘Local_Storage1 call Get’ has finished retrieving its value.

You will need to get those other values in blocks that are executed within the ‘then do’ slot. You can call procedure blocks within ‘then do’ slot, which can help with splitting up the work. Note, however, that if those procedures contain blocks with ‘then do’ slots you will have to ensure that any further processing involving those blocks’ values are done within their ‘then do’ slots.

We know that it can get awkward to program this way and we are working on ways to make it less awkward.


1 Like

OK, got it. I’ll try it this way. I’m just relieved that I’m not going crazy- thought I was missing something simple! Thanks for your help.

Glad to help!