-
Notifications
You must be signed in to change notification settings - Fork 12
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
Logic for returning results from a function that get XHR calls response body #102
Comments
Hello, I may have found a solution:
This does the trick but is not very efficient (20s is a lot but you need a good margin). Is there a way to resolve the promise instead based on a condition? Thank you |
Hi @bakaburg1, We're glad you like crrri! I haven't had time to dive in your example but here is what I would do to create an async counter with R promises: # we can use a closure to define an async counter:
async_counter <- function(resolve_at = 20) {
# initialize objects
count <- 0
resolve_function <- NULL
# build a promise and store the resolve function
pr <- promises::promise(function(resolve, reject) {
resolve_function <<- resolve
})
# build a function to increment the counter
increment_counter <- function() {
count <<- count + 1
if (count >= resolve_at) {
resolve_function(count)
}
}
# return both the counter and the promise:
list(increment_counter = increment_counter, promise = pr)
}
# demo #1 -----------------------------------------------------------------
my_counter <- async_counter(resolve_at = 1)
# check the promise:
my_counter$promise
# increment the counter
my_counter$increment_counter()
# check the promise
my_counter$promise
# demo #2 -----------------------------------------------------------------
my_counter <- async_counter(resolve_at = 1)
my_counter$promise$then(function(value) {
cat("promise resolved, value:", value)
})
my_counter$increment_counter() I hope it will help. |
Thank you! it took me a while but I was able to more or less understand the logic of what you did and integrate it in my function. My problem was to stop a promise started by the crrri client.
|
Hello,
first of all compliments for your library, it's a great evolution over RSelenium (which I didn't like).
I created a function that allows to get the response body of resources requested by a page. The algorithm I made works well in interactive mode since I can wait for the requests to b fulfilled to collect the results, but I have no idea how to do this in an automatic function.
I have no idea if there is a smart way to intercept some relevant event. In alternative I thought of a timer or a counter that stops when a chosen number of resources is collected, but I wouldn't know even how to implement the last two (new to Promises).
Heres a use case:
resources <- get_website_resources('https://app.powerbi.com/view?r=eyJrIjoiM2MxY2RkMTQtOTA3Mi00MDIxLWE1NDktZjlmYTdlNDg0MTdkIiwidCI6IjhkZDFlNmI0LThkYWMtNDA4ZS04ZDhkLTY3NTNlOTgwMDUzMCIsImMiOjl9', url_filter = 'querydata', type_filter = 'XHR')
Thank you!
Angelo
The text was updated successfully, but these errors were encountered: