rstudio / promises

A promise library for R
https://rstudio.github.io/promises
Other
198 stars 19 forks source link

promise_map() does not handle NULL results #47

Open RLesur opened 5 years ago

RLesur commented 5 years ago

In promise_map(), if the function .f returns NULL, the value of the fulfilled promise is incorrect. Here is a minimal reproducible example:

library(promises)

test_promise_map <- function(first_res, second_res) {
  f <- function(index) {
    switch(index, first_res, second_res)
  }
  promise_map(1:2, f) %...>% print()
}

test_promise_map("anything", "anything_else")
#> [[1]]
#> [1] "anything"
#> 
#> [[2]]
#> [1] "anything_else"

test_promise_map(NULL, "anything_else")
#> [[1]]
#> NULL

test_promise_map("anything", NULL)
#> [[1]]
#> [1] "anything"

My guess is the problem is located here: https://github.com/rstudio/promises/blob/852e9be0cd2c91aa72105ed0f2dacfd66d97d6f6/R/utils.R#L152 When this_value is NULL the corresponding element of the results list is removed.