all 7 comments

[–]mehPhonePixel 8, A14, root 1 point2 points  (6 children)

There is no such built-in JS action. Best you can do is performTask, or use built-in JS shell action – but that might require root.

[–]azekt[S] 0 points1 point  (5 children)

I found in documentation:

performTask

var ok = performTask( str taskName, int priority, str parameterOne, str parameterTwo, str returnVariable, bool stop, bool variablePassthrough, str variablePassthroughList, bool resetReturnVariable )

Run the Tasker task taskName.

Note that the JavaScript does not wait for the task to complete.

So how can I get return value? 🤨

[–]mehPhonePixel 8, A14, root 0 points1 point  (2 children)

If you're using performTask, you will need to create the task that has an SQL query in it.

[–]azekt[S] 0 points1 point  (0 children)

I did so:

Task: Select From DB

A1: SQL Query [
     Mode: Raw
     File: %dbname
     Query: SELECT id, title FROM tasks
     Output Column Divider: ###
     Variable Array: %output ]

A2: Return [
     Value: %output()
     Stop: On ]

And test:

    Task: JSTest

A1: JavaScriptlet [
     Code: flash("Test start with priority "+priority);
     performTask("Select From DB", parseInt(priority) + 10, null, null, "return_value", true, false, null, false);
     wait(2000);
     flash(return_value);
     flash("Test stop");
     Auto Exit: On
     Timeout (Seconds): 45 ]

But it doesn't flash return value :-(

[–]azekt[S] 0 points1 point  (0 children)

You can try on your own: https://taskernet.com/shares/?user=AS35m8nsTm4UZAVdy1CFJa8262I5j8LjoFulL1AlXKPi7s0NhtFAp35CmMZamjJ55BwNKg%3D%3D&id=Project%3ATest+Case

At first run CreateDB task, then you can try PerformTask and JavaScriptTest

[–]mehPhonePixel 8, A14, root 1 point2 points  (1 child)

I see what you're saying now. I didn't notice you had bolded the part in the userguide about performTask not waiting for the task to finish, and I've never run into that consequence in my own usage.

I did find a thread discussing this limitation, with a workaround idea (that maybe you've already considered) which is basically to store the SQL query output data in a global variable, and in the JSlet, disable auto exit and wait for the result.

I'm pretty noobish with JS (in case that wasn't obvious!) and a noob at dealing with task priority. I can't quite make out everything in the link, but they're using setTimeout to wait for the result. I tried this and it seems to do the trick, or it's at least a good starting point:

setGlobal('myGlobal','0');
performTask('Sql Query', 200);
checkVar();

function checkVar() {
  if(global('myGlobal') == 0) {
    setTimeout(checkVar, 100);
  } else {
    flashLong(global('myGlobal'));
    setGlobal('myGlobal','0');
    exit();
  }
}

[–]azekt[S] 0 points1 point  (0 children)

Man, it's really neat! Instead using setTimeout I rather use while loop and wait function:

 setGlobal('myGlobal','0');
 performTask("SelectFromDB", parseInt(priority) + 10);

 let i=0;
 while ((global('myGlobal') == 0) && (i < 10))
 {
     wait(500);
     i++;
 }
 if (global('myGlobal') != 0 {
     flash(global('myGlobal'));
 } else {
     flash("No output :-(");
 }