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

Return functions #35

Open
Phara0h opened this issue May 27, 2020 · 4 comments
Open

Return functions #35

Phara0h opened this issue May 27, 2020 · 4 comments

Comments

@Phara0h
Copy link

Phara0h commented May 27, 2020

Any way to do this

addon.foo((string)=>{
  string = "slkdfjsf";
  return string;
})

or this

addon.foo((func)=>{
  func("slkdfjsf")
})
@sehz
Copy link
Collaborator

sehz commented May 28, 2020

not yet, is something you need?

@Phara0h
Copy link
Author

Phara0h commented Jun 2, 2020

Yes this would be needed for porting over a http framework im working on.

@Phara0h
Copy link
Author

Phara0h commented Jun 2, 2020

This is what im looking to write.

request.get('/test/:foo', (req, res)=>{
  
  res({
     status:200,
    body: 'hello'
  })
// or this

res.send('hello');
})

@sehz
Copy link
Collaborator

sehz commented Jun 2, 2020

Hmm, in this case rust fn would something like this:

struct Response {
....
}

struct Request {
....
}

impl Request {
    #[node_bindgen]
    async fn get<F,T,R>(path: String, handler: F)
          where F: FnOnce<Req,Response> -> T,
                 T: Future<Output = Response> + 'static + Send
{
          let req = // create req 
          let resp = // create response
          let response = handler(req,resp).await;      // wait JS to return invoke response
          
          ...do something with response, probably return to client
    }
}

Tricky part is how to map Response which is actually function call from JS point of view.

It would be be easier instead of callback, pass async fn.

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

2 participants