[Solved] FaceGraphics invisible, how to draw it?

I have this code in a window:

this.drawActorFace(actor, 0, 0, 144, 144); // no image is drawn. This method is from Window_Base.Of course no image is drawn because, Javascript loads them asynchronously in the RM code further down the road (in the ImageManager).

So basically, I need to somehow call the this.refresh() again after the image is loaded, or better, not call refresh until it's loaded. How do I hook into that? Is there a method that is called when the bitmap finishes loading or something? Because I rather not loop the refresh or something else checking every frame if a face-bitmap finished loading.

I took a look at how Window_MenuStatus did it but couldn't find it. Because they must have had that same problem and solved it somehow. But how?
Is there a method that is called when the bitmap finishes loading or something?
Yep:
Code:
bitmap.addLoadListener(callback);
I can't get it to work

var alias_silv_skillGem_Scene_Window_Base_drawFace = Window_Base.prototype.drawFace;Window_Base.prototype.drawFace = function(faceName, faceIndex, x, y, width, height){    alias_silv_skillGem_Scene_Window_Base_drawFace.call(this, faceName, faceIndex, x, y, width, height);    bitmap.addLoadListener(this.refresh); // <<<<<<<<<< crash, bitmap is only known in the original drawFace...};Shameless overwrite attempt, but also crashes:

Spoiler
Window_SG_CharacterInfo.prototype.drawFace = function(faceName, faceIndex, x, y, width, height) { width = width || Window_Base._faceWidth; height = height || Window_Base._faceHeight; var bitmap = ImageManager.loadFace(faceName); bitmap.addLoadListener(this.refresh); // <<<<<<<<<< added line. var pw = Window_Base._faceWidth; var ph = Window_Base._faceHeight; var sw = Math.min(width, pw); var sh = Math.min(height, ph); var dx = Math.floor(x + Math.max(width - pw, 0) / 2); var dy = Math.floor(y + Math.max(height - ph, 0) / 2); var sx = faceIndex % 4 * pw + (pw - sw) / 2; var sy = Math.floor(faceIndex / 4) * ph + (ph - sh) / 2; this.contents.blt(bitmap, sx, sy, sw, sh, dx, dy);};
Code below also crashes but this time on the refresh(). Because this.refresh is not yet known to Javascript if the bitmap finishes loading before Javascript finished creating the object I suppose...

Spoiler
Code:
Window_SG_CharacterInfo.prototype.initialize = function(){    ....    this.refresh();    var bitmap = ImageManager.loadFace(Silv.SG_Scene.ActiveActor.faceName());    bitmap.addLoadListener(this.refresh()); //     bitmap.addLoadListener(this.refresh); yields same result as does: bitmap.addLoadListener(refresh);};Window_SG_CharacterInfo.prototype.refresh = function(){    this.contents.clear(); // <<< crash. this.contents is undefined.... Because the this.refresh() will crash if the object hasn't been fully created by JS before the image finishes loading...    this.render();};
partyWindow.prototype.preloadImages = function(){ for(var i = 0; i < $gameParty.members().length; i++){ var bitmap = ImageManager.loadCharacter($gameParty.members()._characterName); bitmap.addLoadListener(function() {this.refresh()}.bind(this)); }; };partyWindow.prototype.drawActorList = function(index) { var rect = this.itemRect(index); var actor = $gameParty.members()[index]; this.drawCharacter(actor._characterName, actor._characterIndex, rect.x + 48 / 2, rect.y + 48); };This is the code I use to preload my actor images then draw them in a window. Works for me. Code is based off of windows selectable but that doesn't matter.
Thanks to both your answers I found the solution (place in initialize):

var preload_bmp = ImageManager.loadFace(Silv.SG_Scene.ActiveActor.faceName()); preload_bmp.addLoadListener(function() {this.refresh()}.bind(this));It seems I messed up the code for adding the addLoadListener. I had to add .bind(this). Also once pre-loaded they stay cached which is good. So buildin-RM-code can also use it.
Sadly that did not quite do the trick. Loading the scene for a 2nd time will crash:

Spoiler
Object.defineProperty(Window.prototype, 'contents', { get: function() { return this._windowContentsSprite.bitmap; // <<<<<<<< crash: TypeError: Cannot read property 'bitmap' of undefined }, set: function(value) { this._windowContentsSprite.bitmap = value; }, configurable: true});called from here:

Window_Selectable.refresh = function(){ if (this.contents) // <<<<<<<<<< here the function is called that causes the crash { this.contents.clear(); this.drawAllItems(); }};
So the default RM-code checks for this.contents, which requires this._windowContentsSprite to be initialized... Maybe the default RM code should have checked that as well?

I solved it by pre-loading the bitmap AFTER the Window_Base has been fully initialized instead of before. Because of the pre-loaded image was already in image-cache, it would attempt to refresh() the Window immediately (before the window even has contents).

Original thread: https://forums.rpgmakerweb.com/threads/solved-facegraphics-invisible-how-to-draw-it.52965/