elixir-nodejs icon indicating copy to clipboard operation
elixir-nodejs copied to clipboard

Handling callbacks and promises

Open gushonorato opened this issue 3 years ago • 3 comments

Hey,

How does elixir-nodejs handle function callbacks and promises? I couldn't find those topics on docs.

Cheers, Gustavo

gushonorato avatar Mar 11 '21 14:03 gushonorato

You can see examples of using promises in the tests here:

https://github.com/revelrylabs/elixir-nodejs/blob/master/test/nodejs_test.exs#L125

from the javascript code here:

module.exports = async function echo(x, delay = 1000) {
  return new Promise((resolve) => setTimeout(() => resolve(x), delay))
}

https://github.com/revelrylabs/elixir-nodejs/blob/master/test/js/slow-async-echo.js

A PR to add this example in the README.md would be nice.

kicktheken avatar Apr 25 '21 17:04 kicktheken

@kicktheken I didn't see this in the readme, so I just did a PR to add this.

kkreine avatar Sep 15 '21 23:09 kkreine

That explains promises, but what about actual callbacks. For example:

module.exports = function echo(x, cb) {
 setTimeout(() => cb(null, x), 2000)
}

I think the only way to interact with code like that ☝️ is to actually wrap it with a promise. We cannot supply callbacks to the node.js function from elixir.

Any advise on handling rejected promises? So far I can't get the elixir code to read the response.

ghost avatar Dec 12 '21 18:12 ghost