cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External CSS

These stylesheets will be added in this order and before the code you write in the CSS editor. You can also add another Pen here, and it will pull the CSS from it. Try typing "font" or "ribbon" below.

Quick-add: + add another resource

Add External JavaScript

These scripts will run in this order and before the code in the JavaScript editor. You can also link to another Pen here, and it will run the JavaScript from it. Also try typing the name of any popular library.

Quick-add: + add another resource

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

            
              <html>
<head>
  <title>Promise.all Example ES6 Version</title>
  <script type="text/javascript">    
    function log(s, elem) {
      console.log(s);  
      if (elem) {
        elem.innerHTML += `<p>${s} </p>`;
      }
    }    
    function doSomething() {
     	let divOutput = document.getElementById('dom_console');
      let btn_default = document.getElementById('btn_default');
      btn_default.disabled = true;
      divOutput.innerHTML = "&nbsp;";
      log("Calling 3 async functions -- wait a few seconds ...", divOutput);
    	var files = ['file1', 'file2', 'file3'];
    	var promises = [];
    	for (var i = 0; i < files.length; i++) {
    		promises.push(fakeAjaxWrapper(files[i]));
    	}    	
    	Promise.all(promises)
    	.then(function(result) {      	
    		for (var i = 0; i < result.length; i++) {
    			log(result[i], divOutput);
    		}
        btn_default.disabled = false;  
    	});              
    }
    window.onload=doSomething;
  </script>
</head>
<body>
  <div class="container-fluid">
    <div class="jumbotron">
      <h1>Promise.all Example - ES6</h1>
      <p>This example builds on my <a href="https://codepen.io/JohnLockwood/pen/qqOQYK" target="_top">earlier example</a> using bluebird's promise library, using ES6 promises instead of those provided by Bluebird to handle waiting for multiple asynchronous calls. "Promise.all" is used to waith for all promises to resolve, at which time we print the results in order.
      </p>
      <p><input type="button" id="btn_default" value="Run again" class="btn btn-round btn-primary btn-lg" onclick="doSomething();" />
        <div id="dom_console">
        </div>
    </div>
  </div>
</body>
</html>
            
          
!
            
              function fakeAjax(url,cb) {
	var fake_responses = {
		"file1": "The first text",
		"file2": "The middle text",
		"file3": "The last text"
	};
	var randomDelay = (Math.round(Math.random() * 1E4) % 2000) + 2000;
	// console.log("Requesting: " + url);
	setTimeout(function(){
		cb(fake_responses[url]);
	},randomDelay);
}

// **************************************
// New (promisify-Compatible) hotness
// Rest of solution in ex5-fixed.html

function fakeAjaxWrapper(file, cb) {
  var p = new Promise((resolve, reject) => {
    fakeAjax(file, text => { resolve(text); })
  });
  return p;                     
}

            
          
!
999px
Loading ..................

Console