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

async calls #11

Closed
dvv opened this issue Mar 16, 2011 · 9 comments
Closed

async calls #11

dvv opened this issue Mar 16, 2011 · 9 comments

Comments

@dvv
Copy link

dvv commented Mar 16, 2011

Hi!

How do I manage a client to call a server-side async function (the one that doesn't return the result but instead calls a given callback)?

TIA,
--Vladimir

@ericz
Copy link
Contributor

ericz commented Mar 16, 2011

Hi Vladimir,

Server side async functions are 100% supported.

Simply pass in a callback.The tricky part is you must also pass in a 2nd callback for the return value.

Here is an example

Server:

var everyone = require("now").initialize(httpServer);

everyone.now.getServerInfo = function(callback){
  doAsync(function(data){
    callback(data);
  });
}

Client:

now.getServerInfo(function(data){
  // this is the callback of the async function
}, function(returnVal){
  // this contains the return value, which will be undefined since the server function doesn't      return anything.
});

@dvv
Copy link
Author

dvv commented Mar 16, 2011

Very well. Thanks!

@scottburch
Copy link

I tried this and it does not seem to work.

@dvv
Copy link
Author

dvv commented Mar 19, 2011

It does work for me.

server:

everyone.now.getServerInfo = function(arg, callback){
  callback('Hello from ' + arg);
}

client:

now.getServerInfo('async function', function(result){
  console.log(result); // 'Hello from async function'
},function(){});

Notice the noop second parameter

@ericz
Copy link
Contributor

ericz commented Mar 19, 2011

Hi guys,

In yesterday's release (v0.3) we have decided to eliminate the necessity of the noop second parameter that dvv has shown. This is because return values are no longer supported. Instead async callbacks should be used.

Considering server code:

 everyone.now.getServerInfo = function(callback){
   callback('Hello');
   return "Bye";
 }

And client code:

In v0.2 you could do this (this is no longer valid in the latest release):

 now.getServerInfo(function(data){
   // data contains 'Hello'
 }, function(returnVal){
   // this contains 'Bye'
 });

But v0.3 this must be changed to

 now.getServerInfo(function(data){
   // data contains 'Hello'
 });

As you can see this gets rid of the confusing noop callback that you would have to use in v0.2. Originally that was used for return values from the server function, but we found people rarely used return values so we decided to eliminate the confusing element in v0.3

@dvv
Copy link
Author

dvv commented Mar 19, 2011

Very well.

@dvv
Copy link
Author

dvv commented Mar 21, 2011

0.3: now.getServerInfo(console.log) throws 'Illegal invocation'. So one can not use natives as callback?

@ericz
Copy link
Contributor

ericz commented Mar 22, 2011

@dvv, yes this is a known issue. It's not possible to do nativeFunction.apply(...) as that throws the illegal invocation

@dvv
Copy link
Author

dvv commented Mar 22, 2011

np

This issue was closed.
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