diff --git a/src/plot_api/plot_api.js b/src/plot_api/plot_api.js index c3860746323..fc9c636345a 100644 --- a/src/plot_api/plot_api.js +++ b/src/plot_api/plot_api.js @@ -337,12 +337,6 @@ Plotly.plot = function(gd, data, layout, config) { Registry.getComponentMethod('updatemenus', 'draw')(gd); } - function cleanUp() { - // now we're REALLY TRULY done plotting... - // so mark it as done and let other procedures call a replot - gd.emit('plotly_afterplot'); - } - Lib.syncOrAsync([ Plots.previousPromises, addFrames, @@ -354,11 +348,12 @@ Plotly.plot = function(gd, data, layout, config) { drawAxes, drawData, finalDraw - ], gd, cleanUp); + ], gd); // even if everything we did was synchronous, return a promise // so that the caller doesn't care which route we took return Promise.all(gd._promises).then(function() { + gd.emit('plotly_afterplot'); return gd; }); }; diff --git a/test/jasmine/tests/plot_api_test.js b/test/jasmine/tests/plot_api_test.js index 05af47433cd..83b218bc7e9 100644 --- a/test/jasmine/tests/plot_api_test.js +++ b/test/jasmine/tests/plot_api_test.js @@ -84,6 +84,21 @@ describe('Test plot api', function() { expect(gd._transitionData._frames[2].name).toEqual('frame3'); }).catch(fail).then(done); }); + + it('should emit afterplot event after plotting is done', function(done) { + var afterPlot = false; + + var promise = Plotly.plot(gd, [{ y: [2, 1, 2]}]); + + gd.on('plotly_afterplot', function() { + afterPlot = true; + }); + + promise.then(function() { + expect(afterPlot).toBe(true); + }) + .then(done); + }); }); describe('Plotly.relayout', function() {