elixir-nodejs
elixir-nodejs copied to clipboard
Handling callbacks and promises
Hey,
How does elixir-nodejs handle function callbacks and promises? I couldn't find those topics on docs.
Cheers, Gustavo
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 I didn't see this in the readme, so I just did a PR to add this.
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.