[Vuejs]-JS – Method executes last even though it's invoked first

0👍

It’s because html2canvas($("#size-chart").get(0)) is returning a promise (maybe just a thenable), which is an async call.

So sizeChartAsImage will run, and html2canvas($("#size-chart").get(0)) will execute. While the script is waiting for that to return it’ll continue to setSizeChart function and run it. And then it’ll return to the code within the then(canvas => callback.

You could either call setSizeChart at the end of the callback. Or, if you’re using ES2017 or greater, you could re-write sizeChartAsImage to be async and await it.

Leave a comment