1

Essentially, I need to extract the dataURL from the canvas and return it.

function getImage() {
  var p = new Promise(function (resolve, reject) {
    html2canvas(document.body).then(function (canvas) {
      var image = canvas.toDataURL("image/png");
      resolve(image);
    });
  });

  p.then(function (image) {
      return image;
    })
    .catch(function (err) {
      console.log(err)
    });
}

Please help to correct my code.

thefourtheye
  • 233,700
  • 52
  • 457
  • 497
William Ku
  • 798
  • 5
  • 17
  • a) [avoid the `Promise` constructor antipattern](http://stackoverflow.com/q/23803743/1048572) b) don't try to synchronously `return` a value when you cannot. There's just no way to do that. Return the promise for the URL instead, and adapt your caller. – Bergi Dec 03 '15 at 05:55
  • Your code can reduced to `function getImage() { return html2canvas(document.body).then(function (canvas) { image = canvas.toDataURL("image/png"); return image; }).catch(function (err) { console.log(err) }); }` Your function is now a promise, use it like this `getImage().then(function(image) { })`. – Shanoor Dec 03 '15 at 06:00
  • thanks @ShanShan for your help – William Ku Dec 03 '15 at 06:56

0 Answers0