Comments (11)
Here is my current workaround btw: http://bit.ly/IcN69W
from meteor.
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.
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.
+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.
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.
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.
+1 for this feature. I want server to do something use 'child process' and do some 'shell' command for me...
from meteor.
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.
Forgot to mention. You also need to call this.unblock(); in the method call for this to work properly.
from meteor.
@scottburch tks, It works well!
from meteor.
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)
- Meteor 3 beta.0: Throwing an error inside of a meteor method doesn't throw on the client
- Cordova iOS - Not working with Vue3 HOT 2
- Meteor3 asyncIterator does not exist on Asynchronous cursor
- [ Meteor v3 ] -`Email.sendAsync` fails, also impacting `accounts-password` methods HOT 1
- Meteor 3 changes the default behavior of Meteor.absoluteUrl() HOT 5
- After calling an async method, sync methods no longer work (Cordova only) HOT 1
- Typo in "Why Meteor?" section
- Top-level await does not seem to work in Meteor 3 HOT 2
- Auto Walk always goes to 0,0
- [Meteor 3] Server-only Method displays "Method stub took too long" warning in browser console when using Meteor.callAsync HOT 3
- Underscore 1.6.0 `_.intersection` bug (Meteor package) HOT 1
- Vite config is parsed incorrectly HOT 2
- Error when installing package `jam:method` HOT 5
- `npm install -g meteor` has overly strict install requirements HOT 2
- Exception in onStop callback: TypeError: handle.stop is not a function HOT 3
- issue of auto compile which is not compiling the project locally HOT 3
- Cannot install dependency from the bundle about "source-map-support" from other nation
- [3.0] insertAsync on client won't persist local document HOT 2
- Underscore _.where function return only one item HOT 2
- Meteor.user() does not work in webApp connect handlers HOT 16
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google β€οΈ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from meteor.