function reduce(functn, intialPromise) {
var proceed = true;
var promise = intialPromise;
do {
functn(promise).then(next => {
proceed = next.proceed;
promise = next.promise;
});
} while(proceed);
return promise;
}
Worker function
let promiseFunction = (promise) => {
let proceed = true;
return promise.then(() => {
// some processing in which proceed can become false
let nextPromise = {};
if(proceed){
nextPromise.proceed = true;
nextPromise.promise = next promise task;
}
else {
nextPromise.proceed = false;
nextPromise.promise = Promise.resolve(true);
}
return Promise.resolve(nextPromise);
});
};
Execution
reduce(promiseFunction, initialPromise).then(() => {
return success;
});
This code is looping continuously.
The idea was to have chained promise. The data on which promise acts is big say 1 GB. but the allocated memory for promise processing is low - 100 MB. So the promise has to run in chain - taking small batches which fit into memory.
Also currently tied to ES6 code, and polyfills/transpiling is not added yet. Hence expecting result in ES6 code.
PS: Not a javascript expert, kindly apologise for blunders