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

Handling callbacks and promises #61

Open
gushonorato opened this issue Mar 11, 2021 · 3 comments
Open

Handling callbacks and promises #61

gushonorato opened this issue Mar 11, 2021 · 3 comments

Comments

@gushonorato
Copy link

Hey,

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

Cheers,
Gustavo

@kicktheken
Copy link

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.

@kkreine
Copy link
Contributor

kkreine commented Sep 15, 2021

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

@ghost
Copy link

ghost commented Dec 12, 2021

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.

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

3 participants