I do something like this. It seems to work for me, but is it OK? Is there any better way?
function myPromise(options) {
return Q.Promise(function(resolve, reject, notify) {
doSomthingAsync(options, function(resp) {
notify(resp);
if (resp.nextPageToken) {
options.pageToken = resp.nextPageToken;
myPromise(options).then(resolve, reject, notify);
} else {
resolve(resp);
}
});
});
}
NOTE: I know mutable options are unwise.
FYI: I'm not sure, but in ReactiveCocoa, there is a same kind of functionality.
-[RACSignal subscribe:]
https://github.com/ReactiveCocoa/ReactiveCocoa/blob/1e97af8f5681b3685770eb30faf090e64c293290/ReactiveCocoaFramework/ReactiveCocoa/RACSignal.h#L115-L131