1

Javascript(let) does not set local variables when manual exit() is called

Create a javascriptlet with content `var test = 1`, and follow it with an action to flash %test. You should see a flash containing "1". Now put `exit();` at the end of the javascriptlet, and you will notice that the variable is not set when the flash action happens.

In summary, JS variables are only translated into Tasker variables when exit() is not called. Is there any way around this? Perhaps a taskerfyVariables() function that could be called before exit() ?

5 replies

The following post describes a workaround.

Context - why would you want to call exit() manually? I wanted to disable the auto exit functionality, as leaving auto exit enabled means you don't get any visibility of errors in your JS code. When it is disabled errors are flashed, but then you need to exit some other way, i.e. by calling exit() when you are done.

Solution: at the top of your code, put the following:

setTimeout(() => exit());

Now once your code completes, exit will be called automatically, BUT any variables you set will still be available in the rest of the task.

Credit to /u/JustRollWithIt on reddit for this solution.

Thanks, that explains it :) I'll get around to it when I can, thanks

I added another JS bug, might be worth considering them simultaneously if you will be looking at the JS code.

https://tasker.helprace.com/i239-javascriptlet-does-not-load-libraries-when-previewing

From the guide:

JavaScript(let): Auto Exit
By default, the JavaScript(let) action will end when the main execution sequence is finished.

If you are using asynchronous code e.g. via setTimeout() or other callbacks, you should deselect Auto Exit. You are then responsible yourself for telling Tasker to continue the task by calling exit().

In any case, execution will stop when the timeout configured for the action is reached.

When Auto exit is enabled, variables amended or created in JavaScript are available in the following actions in the task, as described in the guide:

Local Variables
In JavaScript(let) actions, local variables (all lower case, e.g. %myvar) are directly accessible in the JavaScript without the % sign (e.g. myvar). If the script changes the value, the new value is transparently used by subsequent actions in the task.

The values of new (all lower case) variables declared in JavaScript (with the var keyword) are also available to subsequent actions

So the problem is that variables changed or created in JS are not available to subsequent actions when the JS is terminated by calling exit().

I didn't mention in the original post, but variables are updated correctly if "auto exit" is not selected, but the JS exits by timing out rather than by a call to exit().

Does that clarify it? 

Sorry if this is a dumb question, but what does exit() do? :)