Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Nov 2017
    Location
    Phoenix, Arizona
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Promise doesn't seem to work in Firefox...

    In trying to understand Promises, I put together the following small bit of code:

    Code:
    <script>
    $(document).ready(function()
    	{
    	function get(url) 
    		{
    		return new Promise(
    			function(resolve, reject) 
    				{
    				console.log ("Creating a Promise...");
    			    var Request = new XMLHttpRequest();
    			    Request.open("GET", url, true);
    			    Request.responseType = "arraybuffer";
    				Request.onerror = () => { alert('BufferLoader: XHR error'); }
    				Request.onload = function()
    			    	{
    					if (Request.response) 
    						{
    						console.log("Request completed");
    						resolve(Request.response);
    						}
    					else 
    						reject("Disaster!"); 
    					}
    			    Request.send();
    			    console.log("Request sent...");
                                }
    		    );
    	    }
    	    
    	console.log("About to get...");
    	    
    	get("07.COBOLin'.mp3").then(console.log("Got..."));
    	}
    );
    </script>
    It's my understanding that the get should execute first (with the understanding that there's an async response), but the then should wait for that response. However, that's not the output that's being reported:

    Code:
    About to get...
    Creating a Promise...
    Request sent...
    Got...
    Request completed
    Am I misunderstanding the basic premise here? Or is FireFox not doing it right? (I can't test on Chrome because it won't load a local file with this syntax.)

    Any help would be greatly appreciated!

    Regards,
    Paul

  2. #2
    Master Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    5,842
    Thanks
    26
    Thanked 609 Times in 602 Posts
    The problem is this part:
    Code:
    .then(console.log("Got..."))
    which is simply wrong (then() expects a function to be given to be executed after resolving).

    It should rather read:
    Code:
    .then(function (resolvedValue) {
        console.log("Got...")
    })
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  3. Users who have thanked Dormilich for this post:

    PaulCilwa (Nov 16th, 2017)

  4. #3
    New to the CF scene
    Join Date
    Nov 2017
    Location
    Phoenix, Arizona
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thanks so much! That was it, exactly.


 

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •