简体   繁体   English

为什么我的javascript Promise卡住为“待处理”?

[英]Why is my javascript Promise stuck as “pending”?

I have a complex SVG that is drawn to a canvas, and this task needs to complete before other objects are drawn. 我有一个绘制到画布上的复杂的SVG,需要完成此任务才能绘制其他对象。 Here's a fiddle simulating this: 这是一个模拟这个的小提琴:

https://jsfiddle.net/1hucuLg9/1/ https://jsfiddle.net/1hucuLg9/1/

//draw svg
var promise = new Promise(function(){
  drawSwatch(document.getElementById("mySwatch")); 
  //not typo, canvas context is referenced inside this function due to xml complexity in this fiddle 
});
//draw image
promise.then(function(){
  ctx.drawImage(document.getElementById("myImg",0,0,150,150));
});

You'll note in the fiddle that only the SVG is drawn, not the other image afterwards. 您会在小提琴中注意到,仅绘制了SVG,之后没有绘制其他图像。 The Promise used to draw the SVG is stuck as pending ... even though the inner javascript is all executed. 即使内部javascript全部执行,用于绘制SVG的Promise也停留在待处理状态。 What's going on? 这是怎么回事?

You should resolve the promise. 您应该解决的承诺。 Promise docs should be useful. Promise 文档应该很有用。

var promise = new Promise(function(resolve, reject){
  drawSwatch(document.getElementById("mySwatch"));
  resolve();
});
promise.then(function(){
  ctx.drawImage(document.getElementById("myImg",0,0,150,150));
});

If you want to add myImg only once the image inside drawSwatch has loaded - you can do this 如果你想添加myImg只有一次内部的图像drawSwatch加载-你可以这样做

var canvas = document.getElementById("myCanvas");
var ctx = canvas.getContext("2d");

//hard code variables for example
var vw=300, vh=200, x=20, y=50, w=250, h=50; rx=10, ry=5, bg="#fae", theta=15, midptX=100, midptY=120, blur=5;

//helper function to draw swatch objects
function drawSwatch(currentSwatch, resolve){ // extra parameter
    var data = '<svg xmlns="http://www.w3.org/2000/svg" width="'+vw+'" height="'+vh+'">'+
    '<defs>'+
    '<filter id="f1">'+
    '<feGaussianBlur in="SourceGraphic" stdDeviation="'+blur+'" />'+
    '</filter>'+
    '</defs>'+
    '<rect class="shape" x="0" y="0" width="'+w+'" height="'+h+'" rx="'+rx+'" ry="'+ry+'" transform="translate('+x+','+y+') rotate('+theta+','+midptX+','+midptY+')" fill="'+bg+'" filter="url(#f1)"></rect>'+
    '</svg>';

    //now draw the svg to canvas
    var svg_data = encodeURIComponent(data);
    // create a dataURI version
    var url = 'data:image/svg+xml; charset=utf8, ' + svg_data;
    var img = new Image();
    img.onload = function(){
      ctx.drawImage(img, 0, 0, vw, vh);
      resolve(); // call the passed in resolve function
    }
    img.src = url;
}

//draw svg
var promise = new Promise(function(resolve){ 
  drawSwatch(document.getElementById("mySwatch"), resolve); // call drawSwatch passing in the resolve function
});

promise.then(function(){
  ctx.drawImage(document.getElementById("myImg"),0,0,150,150);
});

added a second parameter to drawSwatch , which is the resolve function from the promise constructor function drawSwatch添加了第二个参数,这是promise构造函数的resolve函数

Now the callback in promise.then wont be called until img.onload is fired 现在img.onload的回调将不会被调用,直到img.onload

working fiddle - in the fiddle, instead of calling resolve directly, I've put it in a setTimout, to show that .then isn't executed until the promise is actually resolved 工作小提琴 -在小提琴,而不是直接调用的决心,我已经把它放在一个setTimout,向人们展示.then没有执行,直到承诺实际上是解决

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM