multiprocessio / datastation

App to easily query, script, and visualize data from every database, file, and API.
https://datastation.multiprocess.io
Other
2.9k stars 112 forks source link

Javascript / SyntaxError: await is only valid in async functions and the top level bodies of modules #183

Closed aheissenberger closed 2 years ago

aheissenberger commented 2 years ago

Node.js v17.5.0 - supports top level async functions and fetch

How to use async functions - eg.

let transform = DM_getPanel(1);
const res = await fetch('https://datastation.multiprocess.io',{method: 'HEAD'})
transform=await res.text()
DM_setPanel(transform);

Error:

Error: [INFO] 2022-02-21T19:57:53 DataStation Runner (Go) development
[INFO] 2022-02-21T19:57:53 Evaling program panel
/private/var/folders/nq/kqg00wpj0yqcdzpzgts7ww440000gn/T/program-panel-2231151971:30
const res = await fetch('https://datastation.multiprocess.io',{method: 'HEAD'})
            ^^^^^

SyntaxError: await is only valid in async functions and the top level bodies of modules
    at Object.compileFunction (node:vm:352:18)
    at wrapSafe (node:internal/modules/cjs/loader:1026:15)
    at Module._compile (node:internal/modules/cjs/loader:1061:27)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1151:10)
    at Module.load (node:internal/modules/cjs/loader:975:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:77:12)
    at node:internal/main/run_main_module:17:47

Node.js v17.5.0
[INFO] 2022-02-21T19:57:53 Failed to eval: exit status 1
eatonphil commented 2 years ago

If you have Node 17.5.0 installed this will work. DataStation uses your existing Node under the hood.

Otherwise you could wrap the panel in an async function and call it:

(async function() {
  let transform = DM_getPanel(1);
  const res = await fetch('https://datastation.multiprocess.io',{method: 'HEAD'})
  transform=await res.text()
  DM_setPanel(transform);
})()
aheissenberger commented 2 years ago

I have v17.6.0 and it did not work.

How can I add an parameter when the binary is called? adding it in settings fails:

[INFO] 2022-02-23T16:43:25 Failed to eval: fork/exec /opt/homebrew/bin/node --experimental-fetch: no such file or directory
eatonphil commented 2 years ago

Yeah you can't do that yet. I'll add support for it in the next release but until then you might be able to write a bash program:

$ cat /usr/local/bin/node-fetch
#!/usr/bin/env bash
set -e
exec node --experimental-fetch "$@"
$ chmod +x /usr/local/bin/node-fetch

And set /usr/local/bin/node-fetch to be the Node program to execute in settings.

aheissenberger commented 2 years ago

I had to use an absolute path to the node binary but it worked.