Last active
December 23, 2015 01:49
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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