Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Should be a way to cancel scheduled callbacks #85

Closed
wch opened this issue Feb 18, 2019 · 1 comment
Closed

Should be a way to cancel scheduled callbacks #85

wch opened this issue Feb 18, 2019 · 1 comment

Comments

@wch
Copy link
Member

wch commented Feb 18, 2019

There should be a way to cancel callbacks that have been scheduled. In some cases, it is known early that the callback won't be needed, and it would be useful to remove it so that objects can be GC'd.

In this example which implements a timeout using promises, we know immediately, when resolve() is called, that timeout function (which calls reject()) won't be needed. However, that function sticks around until 5 seconds has passed, and during that time it holds a reference to the environment e, which prevents e from being GC'd. It's only after the 5 seconds have passed that the callback fires, and reject() is called -- which in this case is a no-op because resolve() has already been called -- that e can be GC'd.

library(promises)

p <- promise(function(resolve, reject) {
  e <- new.env(parent = emptyenv())
  reg.finalizer(e, function(e) message("Finalized"))
  later(function() {
    reject("Failure. :(")
  }, 5)

  resolve("Success! :)")
})

p <- then(p,
  onFulfilled = message,
  onRejected =  message
)

run_now()
gc()
#> Success! :)


# Wait 5 seconds, then run:
gc()
#> Finalized

Related:
rstudio/websocket#40 (comment)

Maybe it could return an ID like JavaScript's setTimeout()? Or perhaps a function that cancels the callback.

@wch
Copy link
Member Author

wch commented Aug 21, 2019

This was implemented in #88.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant