NodeJS scripting & async operations


We’re trying to create a custom service using NodeJS in our DreamFactory 2.2.0 server, but we have a problem with asynchronous operations. It seems that the execution of the script finishes without waiting for asynchronous operations to finish.

For example, when using the following code which uses promises:

    .login("mylogin", "mypassword")
    .then(function(result) {
        console.log("Auth correct: ", result);
        event.response.content = { result: "Authed" };

event.response.content = { result: "Exited" };

It seems that the service always returns { result: "Exited" } but at a later time we can see the "Auth correct" log trace.

Probably this is a similar problem to Help with my nodejs scripting service

¿How can the service be configured to wait for asynchronous operations?

Thanks in advance.

Nobody else experienced this issue?

What about the problem you reported, @jeebee?

I find this very strange because it’s a pretty basic thing, don’t you think, @benbusse?

hey @rbarriuso, i’m looking into it, will let you know when i find out more.

@rbarriuso, To set script response from an async operation use the method below…

event.setResponse(content, statusCode, contentType)

So in your case it will be like …

.login("mylogin", "mypassword")
.then(function(result) {
    console.log("Auth correct: ", result);
    var content = { result: "Authed" };
    event.setResponse(content, 200, 'application/json');
1 Like

You’re totally right, it seems to work properly with event.setResponse(). Thanks!

I fought very long …
Found this post …
Very simple…

Thank you!