diff --git a/src/plots/plots.js b/src/plots/plots.js index ea831406ad2..3e912716eab 100644 --- a/src/plots/plots.js +++ b/src/plots/plots.js @@ -76,12 +76,15 @@ plots.redrawText = function(gd) { plots.resize = function(gd) { gd = Lib.getGraphDiv(gd); - return new Promise(function(resolve, reject) { + var resolveLastResize; + var p = new Promise(function(resolve, reject) { if(!gd || Lib.isHidden(gd)) { reject(new Error('Resize must be passed a displayed plot div element.')); } if(gd._redrawTimer) clearTimeout(gd._redrawTimer); + if(gd._resolveResize) resolveLastResize = gd._resolveResize; + gd._resolveResize = resolve; gd._redrawTimer = setTimeout(function() { // return if there is nothing to resize or is hidden @@ -101,10 +104,17 @@ plots.resize = function(gd) { Registry.call('relayout', gd, {autosize: true}).then(function() { gd.changed = oldchanged; - resolve(gd); + // Only resolve if a new call hasn't been made! + if(gd._resolveResize === resolve) { + delete gd._resolveResize; + resolve(gd); + } }); }, 100); }); + + if(resolveLastResize) resolveLastResize(p); + return p; }; diff --git a/test/jasmine/tests/plots_test.js b/test/jasmine/tests/plots_test.js index 443cdb9ad3e..8c3c3388fed 100644 --- a/test/jasmine/tests/plots_test.js +++ b/test/jasmine/tests/plots_test.js @@ -412,6 +412,31 @@ describe('Test Plots', function() { .then(done); }); }); + + describe('returns Promises', function() { + afterEach(destroyGraphDiv); + + it('should resolve them all', function(done) { + gd = createGraphDiv(); + var p = []; + Plotly.newPlot(gd, [{y: [5, 2, 5]}]) + .then(function() { + gd.style.width = '500px'; + gd.style.height = '500px'; + p.push(Plotly.Plots.resize(gd)); + p.push(Plotly.Plots.resize(gd)); + p.push(Plotly.Plots.resize(gd)); + return Promise.all(p); + }) + .then(function(v) { + // Make sure they all resolve to the same value + expect(v[0]).toEqual(v[1]); + expect(v[1]).toEqual(v[2]); + }) + .catch(failTest) + .then(done); + }); + }); }); describe('Plots.purge', function() {