rstudio / promises

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

Should error if lhs is not a promise object #4

Closed wch closed 6 years ago

wch commented 6 years ago

For example, I had some code where I was essentially doing something like this:

function(resolve, reject) {
  resolve(1)
} %...>% (function(x) {
  print(x)
})

It didn't give any errors, but it certainly didn't work as I had expected, and it took me a long time to figure out why (async stuff is hard enough to reason about as it is).

Alternatively, it would be nice if the lhs could be promoted to a promise in cases like this.

jcheng5 commented 6 years ago

This is a question of R language precedence. The entire rest of the snippet after function(resolve, reject) is being treated as the body. Like this:

function(resolve, reject) {
  {
    resolve(1)
  } %...>% (function(x) {
    print(x)
  })
}

In cases where the syntax is correct (i.e. put parens around the first anonymous function) then you get the expected error: "Don't know how to convert object of class function into a promise"