Git Product home page Git Product logo

Comments (9)

ericz avatar ericz commented on June 20, 2024

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.
});

from now.

dvv avatar dvv commented on June 20, 2024

Very well. Thanks!

from now.

scottburch avatar scottburch commented on June 20, 2024

I tried this and it does not seem to work.

from now.

dvv avatar dvv commented on June 20, 2024

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

from now.

ericz avatar ericz commented on June 20, 2024

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

from now.

dvv avatar dvv commented on June 20, 2024

Very well.

from now.

dvv avatar dvv commented on June 20, 2024

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

from now.

ericz avatar ericz commented on June 20, 2024

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

from now.

dvv avatar dvv commented on June 20, 2024

np

from now.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.