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 Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

Quick-add: + add another resource

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

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