jeroen / V8

Embedded JavaScript Engine for R

Home Page:https://cran.r-project.org/package=V8

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Wait for all promises to complete?

jeroen opened this issue · comments

Now that we support promises, we may need to update the R api to not return immediately when the code has been executed, but wait for outstanding tasks to complete?

For example the code below returns to R immediately when demo() returns, even though a promise is running in the background.

library(V8)
ctx <- v8()
ctx$source('https://unpkg.com/wasm-feature-detect/dist/umd/index.js')
ctx$eval('wasmFeatureDetect.simd().then(console.log)')

I think we basically need a top-level await but JavaScript specifically does not allow that.

When I run this code, it never prints the second message, "Two seconds later.".

I believe this is because there's no setTimeout() function in plain V8.

library(V8)
ctx <- v8()
ctx$eval("setTimeout")
#> Error in context_eval(join(src), private$context, serialize) : 
#>   ReferenceError: setTimeout is not defined

For reference:
https://stackoverflow.com/questions/12335222/settimeout-and-v8

You are right, there is no setTimeout. I updated the code to the example I had originally.