Proper retrieval of Promise outcomes

I am facing a challenge with loading textures in a class. I want the class to be called only after all textures have been loaded successfully. While I have managed to load the textures, I am unsure about where and how to call the class at the right time for it to be returned to the main class. To demonstrate my issue clearly, I have condensed the code to focus on the essential parts.

class Environment { 
    constructor(params) {
                        
        this._Init(params);         
    }

    static init(params) { 
            
        const getTextures = () => new Promise((resolve, reject)=>{ 
            const manager = new THREE.LoadingManager(()=>resolve(textures)); 
            const loader = new THREE.TextureLoader(manager); 
            const textures = [ 
                './resources/simplex-noise.png',
                //next texture,
                //next texture,
                //next texture,
            ].map(filename=>loader.load(filename)); 
        }); 
        getTextures().then(tex => {
            return new Environment({params, tex}); //I'm encountering an issue here.
            //I need "new Environment({params, tex});" as return for "static init"
        });

        //return new Environment(params);  //this is working but here i have no preloaded tex
    } 



    _Init(params) {
        this.params_ = params;

        //....          
    
    }
}



class Main {
    constructor(){
        this._Initialize();
    }

    _Initialize() {   
        //...
        this.OnAppStarted_();
        //...
    }

    OnAppStarted_() {
        this.LoadControllers_();
    }


    LoadControllers_() {
  
        this.obj = Environment.init({
            //params
        });
        
        //...
        
    }
    
    //...
    
}

Answer №1

class World { 
constructor(data) {
                    
    this._Initialize(data);         
}

static initialize(data) { 
        
    // ...
    // include a return statement
    return fetchTextures().then(textures => {
        return new World({data, textures}); //there seems to be an issue here.
        
    });
} 

}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Encountering problem when trying to upload several images at once using a single input in CodeIgniter with

I'm attempting to use CodeIgniter and AJAX to upload multiple images using a single input field. Here's the code I have so far: HTML: <input type="file" name="files[]" id="file" multiple /> AJAX: $("#addItems").on("submit",function(e) { ...

What is the best way to confirm if a Json response is empty or not?

{"PatientPastMedicalHistoryGetResult":{"PastMedicalHistory":[]}} The PastMedicalHistory object does not contain any values. How can I verify if it is empty? ...

Retrieve information from a JSON file using a Node.js server

As a JavaScript beginner, I am trying to figure out how to fetch data from a JSON object located on my localhost. Is there a better way to accomplish this task? fetch('http://localhost:3000/show') .then(result => { console.log( ...

Updating the active navbar link with JavaScript in ASP.NET Core Razor Pages

When navigating through my Razor Pages, I need to dynamically change the color of the navbar links. I attempted using JavaScript, but encountered issues with the pages getting rerendered each time, preventing me from toggling the elements. Here's wha ...

Retrieve Vuejs template by either module or ID

In my .Vue file, I have defined a template along with its subcomponents with the intention of allowing customers to override this template without needing to modify any javascript code. If there exists an element with id="search-result", then that element ...

Disregard keys with null values when making a post request using react and axios

As a beginner in JavaScript, I am facing an issue where I do not want to include keys with null values when sending metadata. For example, I should only send the filepath as it has a value, and omit tags and owner which are null. How can I achieve this? ...

show tab focus outline only

In search of a straightforward and effective method for focusable elements to display an outline only when the tab key is pressed, without showing it when using a mouse in React applications. (looking for something similar to :focus-visible that function ...

Integrating a complex three.js object into A-Frame leads to unforeseen flickering issues with its child components

Currently, I am integrating an augmented reality experience into my web app that was originally built using three.js I have a complex object (an Object3D instance with multiple meshes) that I am trying to place into A-Frame, but I am encountering unexpect ...

Incorporating 'unsafe-eval' into the current chrome extension

I have a popular Chrome extension available in the Chrome store that is used by numerous people. Recently, I integrated alasql which requires the use of the eval function, leading me to need to enable unsafe-eval in the content_security_policy. I'm cu ...

Track the status of an extensive MySQL query using PHP

Looking for a way to show the progress percentage of a lengthy mysql query using php? Well, you can create a filter button in your database that triggers a javascript function through ajax and calls a php file. function show(){ $.ajax({ ...

How to stop a loop of method calls that return a Promise<any> in TypeScript

My current issue involves a loop in which a method is called, and the method returns an object of type Promise<any>. I need to break the loop if the response from the method is correct. However, using the break statement does not stop the loop as exp ...

Using PHP to read an image blob file from an SVG

Currently, I am utilizing the Raphael JS library on the client-side to generate a chart in SVG format. However, my goal is to make this chart downloadable, which poses a challenge since SVG does not natively support this feature. Initially, I attempted to ...

Display a pop-up when the notification is clicked

Can a desktop notification trigger an extension popup to open upon click? Check out the code snippet provided below: var notification = window.webkitNotifications.createNotification(); notification.addEventListener('click', function() { not ...

Verify if the key-value pair can be found within a multi-layered object

Is it feasible to identify the existence of a key/value pair in any type or level of object, regardless of its structure being unknown beforehand? For instance: Pair to find: "kod": "REVH" Object: { "names": [{ "name1": "xxx", "name2": "yyy", ...

Utilizing the code data:post.url from a blogger to create a clickable link paired with an image button

My website is logjik.com. I have implemented two social buttons in my posts, which also appear on my main page. I chose this solution because even though I am on the home page, I wanted to be able to share the link to my post (similar to how 9gag functions ...

Calculate a range while meeting a specific condition using JavaScript

I am currently working on a project in node.js involving the leap motion technology. I need to create a counter that increments by +1 inside a span element every time the numberOfFingers property of a user's hand is equal to 5. Can someone provide gui ...

steps for closing when i click the x button

I am currently working on a task list project, and I am encountering an issue where the close button does not function as expected when clicked. My expectation is that upon clicking the close button, the corresponding item should close. ...

Can the jQuery Cycle Plugin: scrollHorz with Prev/Next automatically scroll without any input from the user?

Currently implementing the jQuery cycle plugin: jquery.cycle.all.min.js - newest version (2.8.8) available at http://malsup.com/jquery/cycle/ The scrollHorz effect is being used with previous and next links, functioning perfectly except for one issue: upo ...

How to determine if a variable has been declared in JavaScript but not assigned any value

Is there a way to check if a variable has been declared but not assigned a value? I've looked at similar questions and they all seem to recommend using typeof myVar !== 'undefined' However, this always returns false because even when decl ...

Details regarding the enthusiastic execution of Promise callbacks

Is there a specific specification or implementation detail that dictates how quickly the callbacks of a promise are evaluated? For example, if I have: var promise = new Promise((resolve) => { resolve() }); console.log(promise); // there is no p ...