I’m developing an organisational chart. I’m looking for tips on restructuring how I handle two es6-promises for a minor performance gain.
On the click event, I do 3 things sequentially:
- Play first animation (promise)
- Fetch all data (promise)
- Play last animation
Ideally, I would like:
- Begin fetching data
- Play first animation
- Play last animation (requires the fetched data, but can’t play before the first animation has ended)
Here is the code in question:
/* Click happened! */
orgWrap.shiftRow({ row: clickY, from: clickX, to: focusX }).then(() => {
/* Shiftrow is a promise triggering an animation,
it resolved on 'transitionend' */
fetch(`api/org/${target.id}/1`).then(result => {
/* a function placing result in the DOM runs
After data finished loading */
orgWrap.populateRow({ data: result.children, row: clickY+1 });
/* The animation which plays last, when shiftRow and fetch has settled */
});
});
As it stands now, there is an occasional unnecessary delay between shiftRow
and populateRow
because it fetches all data after the first animation is done and then runs the final animation, instead it should begin to fetch data on the initial click. Any tips on how I can refactor and better utilise promises and the power of async? I prefer to keep the functions which are promises to stay promises, the rest I am willing to change. Any input is greatly appreciated.