Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/377.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在承诺中包装异步函数?_Javascript_Typescript_Promise_Pixi.js - Fatal编程技术网

Javascript 如何在承诺中包装异步函数?

Javascript 如何在承诺中包装异步函数?,javascript,typescript,promise,pixi.js,Javascript,Typescript,Promise,Pixi.js,我想把一个异步函数包装成一个承诺,这样它就可以同步运行。我需要这样做,因为我需要在继续执行程序之前检索依赖于异步函数的结果 这是我的相关代码: export abstract class OperationElement extends Sprite { private imagePath; abstract setPosition(mousePosition?, backgroundHeight?, backgroundWidth?) : void; loadTe

我想把一个异步函数包装成一个承诺,这样它就可以同步运行。我需要这样做,因为我需要在继续执行程序之前检索依赖于异步函数的结果

这是我的相关代码:

export abstract class OperationElement extends Sprite {

    private imagePath;

    abstract setPosition(mousePosition?, backgroundHeight?, backgroundWidth?) : void;

    loadTexture(){
         var _texture = PIXI.Texture.fromImage(this.imagePath);
         this.texture = _texture;
         console.log(this.height);
    }

    setImagePath(path : string){
         this.imagePath = path;
    }
}
异步运行的部分是行
var\u texture=PIXI.texture.fromImage(this.imagePath)

加载纹理时,我可以获得纹理的高度。我需要纹理的高度,然后才能继续执行我的程序。我如何用承诺来包装它,使它同步运行

我在这里看了一些其他的问题,最相关的问题有一大堆被删减的过时答案,所以我回避了。

loadTexture():Promise{
loadTexture():Promise<PIXI.Texture> {  
     return new Promise<PIXI.Texture>((resolve, reject) => {
         var image = new Image();
         image.src = this.imagePath;
         image.onload = () => {
             console.log(image.height);
             this.texture = PIXI.Texture.from(image);
             resolve(this.texture);
         }
         image.onerror = e => reject(e);
     });
}
返回新承诺((解决、拒绝)=>{ var image=新图像(); image.src=this.imagePath; image.onload=()=>{ 控制台.日志(图像.高度); this.texture=PIXI.texture.from(图像); 解析(这个纹理); } image.onerror=e=>拒绝(e); }); }
loadTexture():承诺{
返回新承诺((解决、拒绝)=>{
var image=新图像();
image.src=this.imagePath;
image.onload=()=>{
控制台.日志(图像.高度);
this.texture=PIXI.texture.from(图像);
解析(这个纹理);
}
image.onerror=e=>拒绝(e);
});
}

您只需要一个简单的回调

onTextureLoaded(){

    console.log( this.texture.height )
    this.texture.off('update', this.onTextureLoaded, this);

}

loadTexture(){
    var _texture = PIXI.Texture.fromImage(this.imagePath);
    this.texture = _texture;

    this.texture.on('update', this.onTextureLoaded, this);

}

您只需要一个简单的回调

onTextureLoaded(){

    console.log( this.texture.height )
    this.texture.off('update', this.onTextureLoaded, this);

}

loadTexture(){
    var _texture = PIXI.Texture.fromImage(this.imagePath);
    this.texture = _texture;

    this.texture.on('update', this.onTextureLoaded, this);

}


fromage
返回什么?@Phil,
fromage
返回一个
Texture
对象,它只是一个内置在pixi.js中的对象。如果您感兴趣,可以在此处阅读:。最重要的是,我需要在继续执行程序之前返回
Texture
对象。
PIXI.Texture.fromImage
已经同步返回
纹理了,不是吗?你不需要承诺或者我遗漏了什么?@Hereblur,不,
PIXI.Texture.fromImage
异步返回纹理,
fromImage
返回什么?@Phil,
fromImage
返回一个
纹理
对象,它只是一个内置在PIXI.js中的对象。如果您感兴趣,可以在此处阅读:。最重要的是,我需要在继续执行程序之前返回
Texture
对象。
PIXI.Texture.fromImage
已经同步返回
纹理了,不是吗?你不需要承诺,还是我遗漏了什么?@Hereblur,不,
PIXI.Texture.fromImage
异步返回纹理在这种情况下,
Image
对象是什么?它不是pixi.js对象,所以它真的需要吗?@PythonNewb它是
HTMLImageElement
,html内置对象。由于图像是在web中异步加载的,所以需要对其进行包装以获得其大小(仅在加载后可用)。而且
PIXI.Texture.fromImage
是同步的,它在内部处理异步加载过程。我想我明白了。在哪里指定纹理?是否应在resolve函数中执行此操作?类似于解析(var\u texture=PIXI.texture.from(image);this.texture=\u texture)
?@PythonNewb,请参见编辑后的答案。解析就像一个
回调
,而只针对成功结果,而拒绝针对错误。所以
resolve(var\u-texture=PIXI.texture.from(image);this.texture=\u-texture)
不起作用。谢谢。线条
解析(this.texture)
做什么?在这种情况下,
图像
对象是什么?它不是pixi.js对象,所以它真的需要吗?@PythonNewb它是
HTMLImageElement
,html内置对象。由于图像是在web中异步加载的,所以需要对其进行包装以获得其大小(仅在加载后可用)。而且
PIXI.Texture.fromImage
是同步的,它在内部处理异步加载过程。我想我明白了。在哪里指定纹理?是否应在resolve函数中执行此操作?类似于解析(var\u texture=PIXI.texture.from(image);this.texture=\u texture)
?@PythonNewb,请参见编辑后的答案。解析就像一个
回调
,而只针对成功结果,而拒绝针对错误。所以
resolve(var\u-texture=PIXI.texture.from(image);this.texture=\u-texture)
不起作用。谢谢。行
解析(this.texture)
做什么?这也很有效。我希望避免回调,因为回调会很快使代码变得混乱。出于某种原因,将高度指定给my
OperationElement
类中的class属性,然后尝试检索它会导致
未定义的
,尽管这也很有效。我希望避免回调,因为回调会很快使代码变得混乱。由于某些原因,将高度指定给my
OperationElement
类中的class属性,然后尝试检索它会导致出现未定义的