Streaming Deferred

Streaming Deferred

I am writing a library that wraps a web service api. The API returns paginated results-- i.e. I have to make an ajax call per page.

I'd like to present my library's users with the partial results as they come in rather than waiting until the end, but I'd also like to give them a Promise representing the end of the stream of pages (or error). I could have the library take a callback argument as a parameter, and then feed updates to the callback, but use a Promise for completion. This seems ugly to me since users have to deal with result processing in two very different ways. Also, the streaming update would have fewer features than the completion notification: without extra coding there can be only one streaming listener, and they have to be registered up-front.

What would be interesting is a StreamingDeferred/Promise:

StreamingDeferred.update(): Add handlers to be called when partial results are ready. Handlers may be registered at any point in the lifecycle of the deferred object. Handlers registered after partial results are already ready will be brought up-to-date with those results. Handlers may be called more than once over the lifetime of the deferred object.

I would expect a typical usage of this would be that the primary payload arrives in chunks via update() handlers, and the done()/fail() handlers exist only as end of stream markers.

I'm not sure what the best naming would be. Perhaps update() for the handler registration, and progress() for the invoker?