Misleading JSGI Promise Docs

6 views
Skip to first unread message

James Cook

unread,
Nov 16, 2016, 8:01:08 PM11/16/16
to RingoJS
In the Promise docs, there is an example that I don't think works under any circumstances.

exports.asyncAction = function(request) {
  var response = new Deferred();
  setTimeout(function() {
      response.resolve({
          status: 200, headers: {}, body: ["Delayed"]
      });
  }, 1000);
  return response.promise;
}

I don't see any code that would allow a promise to be returned to any of the JSGI methods and be able to handle it. In fact, I would really like this to exist. I was hoping I could just 'yield asyncAction()' to get the result, but that doesn't seem to work with this version of Promise. 

It seems like this behavior bugged me quite a bit 3.5 years ago also.

James Cook

unread,
Nov 16, 2016, 8:30:05 PM11/16/16
to RingoJS
Just like 3.5 years ago, I was able to synchronously wait for a WorkerPromise to resolve. This will let me run asynchronous code inside my application, but still allow my stick middleware to handle the response. But it is really ugly.

function handleWorkerPromise ( request ) {
    var wp = new WorkerPromise ( module.resolve ( './nls_worker' ), { request: request }, true );
    wp.wait ( 5000 )
    var result
    wp.then ( function ( payload ) {
                  result = json ( payload )
              },
              function ( error ) {
                  result = error
              }
    )
    return result
}

Furthermore, if my application code needs to yield for a promise (promise.wait() seems to be the only way to do this in Ringo), I will have to repeat this exercise for each of those types of interactions, and that doesn't seem very friendly.


Philipp Naderer / Botic

unread,
Nov 18, 2016, 11:06:11 AM11/18/16
to RingoJS
Hi James!

I filed an issue on Github: https://github.com/ringo/ringojs/issues/357 My hope is to fix it in the next days, but since we have the third attempt to elect the Austrian President early December it could take a little bit to look at this topic.

Bye
Philipp
Reply all
Reply to author
Forward
0 new messages