i'm not sure of how adequately achieve desired control flow using promises/bluebird.
essentially have database x 'tasks' stored , each needs loaded , executed sequentially. don't want run more 1 task concurrently , entire code must continue executing indefinitely.
i have achieved following code far:
export default function syncloop() { getnexttaskrunner().then((taskrunner) => { if (taskrunner) { taskrunner.starttask() .then(syncloop) .catch((error) => { throw new error(error); }); } else { syncloop(); } }); }
getnexttaskrunner() loads , resolves next task database (calc'd based on timestamps). or resolves null (no task avail).
taskrunner.starttask() resolves null when full task has completed.
i've been advised way structured (recursive /w promises) lead stack issues after has been running time.
what i've thought doing restructure like:
let running = false; setinterval(() => { if (!running) { running = true; getnexttaskrunner().then((taskrunner) => { if (taskrunner) { taskrunner.starttask() .then(() => { running = false; }) .catch((error) => { log.error(error); }); } else { running = false; } }); } }, 5000);
or yet possibility, using event emitters in form?
task.on('complete', nexttask());
thoughts , advice appreciated!
what stack issues? way you've written code fine long getnexttaskrunner
async (i.e. gives control main loop @ point, e.g. if async io). there no recursion in code in case. whoever told mistaken.
though might want add settimeout
somewhere won't flood db requests. plus if getnexttaskrunner
no longer sync (due example in memory caching):
export default function syncloop() { settimeout(() => { getnexttaskrunner().then((taskrunner) => { if (taskrunner) { taskrunner.starttask() .then(syncloop) .catch((error) => { throw new error(error); }); } else { syncloop(); } }); }, 2000); }
Comments
Post a Comment