Skip to content

Instantly share code, notes, and snippets.

@apaprocki
Last active Dec 23, 2015
Embed
What would you like to do?
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