Skip to content

Instantly share code, notes, and snippets.

@apaprocki
Last active December 23, 2015 01:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save apaprocki/6562742 to your computer and use it in GitHub Desktop.
Save apaprocki/6562742 to your computer and use it in GitHub Desktop.
Mockup of module API that returns a handle which is then used to read partial responses as promises in a generator function.
exports.Session.prototype.request =
function(uri, name, request, label) {
var current = Q.defer();
var handle = {
// When 'current' is set to 'undefined', it indicates that there are
// no more responses and 'readResponse' should return 'undefined'.
readResponse: function(cb) {
return current ? current.promise : Q(undefined);
}
};
this.session.request(uri, name, request, label,
function(err, response) {
if (err) {
current.reject(err);
} else {
current.resolve(response);
current = response ? Q.defer() : undefined;
}
});
return handle;
}
Q.async(function*() {
// Setup session
// Get a handle to the request.
var requestHandle = session.request('//blp/apiflds',
'FieldSearchRequest', { searchSpec: 'price' }, undefined);
// Loop through any partial responses, stopping after final is returned.
var response;
while ((response = yield requestHandle.readResponse())) {
console.log(util.inspect(response, { showHidden: true, depth: null }));
}
// Stop and destroy the session.
})().done();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment