Node.JS Procedure block until complete

#1

All,

I am using Autobahn in Node.JS to create some services. The snag I am hitting is that the autobahn library is expecting me to synchronously return something BUT my service is making asynchronous calls to other URIs. Here is a basic example (please ignore syntax errors. Meant for illustration)

function asyncFunction(args, callback) {
session.call(“com.example.test”, args,{}).then(
function(res) {
callback(res.kwargs);
}
);
}

session.register(“com.example.procedure”,function(args, kwargs) {
asyncFunction(args, function(kwargs) {
// HAVE VALUE HERE
}

// NEED TO RETURN VALUE HERE
});

``

In Python I just use inline callbacks and this is handled. For JS, do I need to return a Promise or something like that? I can think of some hacky ways to do this with timers but I don’t want to resort to that yet.

0 Likes

#2

Hi Eric,

the idiom ("transform a callback into a promise") is this:

session.register("com.example.procedure",function(args,kwargs){
     // create promise here
     d = ...
     asyncFunction(args, function (kwargs) {
        // HAVE VALUE HERE
        d.resolve(RESULT);
     }
     // NEED TO RETURN VALUE HERE
     return d.promise;
});

I'm on mobile an can't lookup the exact syntax, but AutobahnJS has the underlying promise library (whenjs) exposed on it's public interface for exactly such uses.

/Cheers,
/Tobias

PS: This is a repeating question .. I have answered it somewhere on the list .. we should document it ..

···

Am 11.12.2015 um 15:52 schrieb Eric Chapman:

All,

I am using Autobahn in Node.JS to create some services. The snag I am
hitting is that the autobahn library is expecting me to synchronously
return something BUT my service is making asynchronous calls to other
URIs. Here is a basic example (please ignore syntax errors. Meant for
illustration)

>
functionasyncFunction(args,callback){
    session.call("com.example.test",args,{}).then(
function(res){
          callback(res.kwargs);
}
);
}

...

session.register("com.example.procedure",function(args,kwargs){
    asyncFunction(args,function(kwargs){
// HAVE VALUE HERE
}

// NEED TO RETURN VALUE HERE
});
>

In Python I just use inline callbacks and this is handled. For JS, do I
need to return a Promise or something like that? I can think of some
hacky ways to do this with timers but I don't want to resort to that yet.

--
You received this message because you are subscribed to the Google
Groups "Autobahn" group.
To unsubscribe from this group and stop receiving emails from it, send
an email to autobahnws+...@googlegroups.com
<mailto:autobahnws+...@googlegroups.com>.
To post to this group, send email to autob...@googlegroups.com
<mailto:autob...@googlegroups.com>.
To view this discussion on the web visit
https://groups.google.com/d/msgid/autobahnws/3f5563ef-b0db-40eb-8e24-4fdf04871c44%40googlegroups.com
<https://groups.google.com/d/msgid/autobahnws/3f5563ef-b0db-40eb-8e24-4fdf04871c44%40googlegroups.com?utm_medium=email&utm_source=footer>.
For more options, visit https://groups.google.com/d/optout.

0 Likes

#3

Thanks Tobias, I searched for awhile but couldn’t find it. I agree you should put it in the programming guide. Thanks for your help.

···

On Friday, December 11, 2015 at 8:52:43 AM UTC-6, Eric Chapman wrote:

All,

I am using Autobahn in Node.JS to create some services. The snag I am hitting is that the autobahn library is expecting me to synchronously return something BUT my service is making asynchronous calls to other URIs. Here is a basic example (please ignore syntax errors. Meant for illustration)

function asyncFunction(args, callback) {
session.call(“com.example.test”, args,{}).then(
function(res) {
callback(res.kwargs);
}
);
}

session.register(“com.example.procedure”,function(args, kwargs) {
asyncFunction(args, function(kwargs) {
// HAVE VALUE HERE
}

// NEED TO RETURN VALUE HERE
});

``

In Python I just use inline callbacks and this is handled. For JS, do I need to return a Promise or something like that? I can think of some hacky ways to do this with timers but I don’t want to resort to that yet.

0 Likes

#4

Added issues for the documentations - https://github.com/crossbario/autobahn-js/issues/172 and https://github.com/crossbario/crossbardocs/issues/50

···

Am Freitag, 11. Dezember 2015 19:33:23 UTC+1 schrieb Eric Chapman:

Thanks Tobias, I searched for awhile but couldn’t find it. I agree you should put it in the programming guide. Thanks for your help.

On Friday, December 11, 2015 at 8:52:43 AM UTC-6, Eric Chapman wrote:

All,

I am using Autobahn in Node.JS to create some services. The snag I am hitting is that the autobahn library is expecting me to synchronously return something BUT my service is making asynchronous calls to other URIs. Here is a basic example (please ignore syntax errors. Meant for illustration)

function asyncFunction(args, callback) {
session.call(“com.example.test”, args,{}).then(
function(res) {
callback(res.kwargs);
}
);
}

session.register(“com.example.procedure”,function(args, kwargs) {
asyncFunction(args, function(kwargs) {
// HAVE VALUE HERE
}

// NEED TO RETURN VALUE HERE
});

``

In Python I just use inline callbacks and this is handled. For JS, do I need to return a Promise or something like that? I can think of some hacky ways to do this with timers but I don’t want to resort to that yet.

0 Likes