GithubHelp home page GithubHelp logo

Comments (11)

dsummersl avatar dsummersl commented on May 5, 2024

Here is my current workaround btw: http://bit.ly/IcN69W

from meteor.

ajoslin avatar ajoslin commented on May 5, 2024

If the caller has a function as its last parameter, the callback will be called by the server with the return value or thrown error.

Here's an example (hope you don't mind the coffeescript). Try it out:

# Server
Meteor.methods
  stuff: (value) ->
    if value == 1
      # given as result in callback
      return "good!" 
    else
      # thrown meteor.errors are given as error in callback
      throw new Meteor.Error 404, "value should be 1, bro" 
# Client
Meteor.call 'stuff', 1, (error, result) ->
  console.log 'expected: error=undefined, result="good"'
  console.log 'actual:', error, result

Meteor.call 'stuff', 2, (error, result) ->
  console.log 'expected: error=Meteor.error, result=undefined'
  console.log 'actual:', error, result

http://docs.meteor.com/#methods_header

from meteor.

dsummersl avatar dsummersl commented on May 5, 2024

Hi ajoslin,

This feature request is for the server side, actually, not the client side. As you point out the client side supports both sync and async methods for the client to call a server method. But the server method is currently only sync.

This can be a bit of a problem if the server side implementation relies on some other 3rd party async libraries. In my case I wanted to use mongoose. But it could be a number of other libraries, as the asynchronous callback is extremely common in javascript/node.js. The link I provided shows one way of working around the problem using the fibers-promise library, but it would be nice if the meteor API provided a way of implementing server side method results in a sync as well as async way.

from meteor.

jacobfike avatar jacobfike commented on May 5, 2024

+1 for this.

Here is another use case. I have a login form. When I submit this form I use Meteor.call('login', username, password, loginCallback); On the server, the login method needs to validate against an LDAP server, which requires an async call. However, I can't get the method to wait for the callback, so my loginCallback function gets called almost immediately, before the LDAP check has even occurred, so it does no good.

from meteor.

lukestanley avatar lukestanley commented on May 5, 2024

I need to call http-get to download something and cache locally and so I must also use a callback to know when it returns a cached filename.
May use something like this: http://stackoverflow.com/a/10677578/122364

from meteor.

avital avatar avital commented on May 5, 2024

If you are just firing an http request, our http package supports a synchronous API. http://docs.meteor.com/#meteor_http_call

If you need to make an async call for some reason (for example if you're firing more than one at the same time, or using an npm module), you can use Futures to wait until your async call is complete and then use a standard return statement. Something along the lines of the following answer: http://stackoverflow.com/a/11510874/1352190.

from meteor.

neekey avatar neekey commented on May 5, 2024

+1 for this feature. I want server to do something use 'child process' and do some 'shell' command for me...

from meteor.

scottburch avatar scottburch commented on May 5, 2024

For anyone who needs it. I created this temp solution to the problem which works really well.

function runSync(func) {
    var fiber = Fiber.current;
    var result, error;

    var args = Array.prototype.slice.call(arguments, 1);

    func.apply(undefined, [cb].concat(args));
    Fiber.yield();
    if (error) throw new Meteor.Error(500, error.code, error.toString());
    return result;

    function cb(err, res) {
        error = err;
        result = res;
        fiber.run();
    }
}

Use it like this.

runSync(myFunction, arg1, arg2);

function myFunction(cb, arg1, arg2) {
    // do my async thing and then call cb(err, result);
}

from meteor.

scottburch avatar scottburch commented on May 5, 2024

Forgot to mention. You also need to call this.unblock(); in the method call for this to work properly.

from meteor.

neekey avatar neekey commented on May 5, 2024

@scottburch tks, It works well!

from meteor.

Xample avatar Xample commented on May 5, 2024

Meteor call supporting callback http://docs.meteor.com/#/full/meteor_call
Making use of a promise would just do it…
something like

function doStuff()
{
var promise = new Promise()
{
Meteor.call("methodName", function(error, result)
{
if (error) promise.reject(error);
else promise.resolve(result);
})
}
return promise;
}

Usage:

doStuff().then(function(result){ /* Use the result */ })

PS: code not tested
My favorite package for promises is : https://github.com/meteor/promise

Alternative: the following package does this for you:
https://atmospherejs.com/okgrow/promise

Usage:

Meteor.promise('methodName')
   .then(function(result){ console.log(result)})
   .catch(function(err){ console.error(err))}))

from meteor.

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.