{"id":33632,"date":"2017-10-11T03:01:36","date_gmt":"2017-10-10T17:01:36","guid":{"rendered":"http:\/\/www.rjmprogramming.com.au\/ITblog\/?p=33632"},"modified":"2017-10-11T19:39:10","modified_gmt":"2017-10-11T09:39:10","slug":"promise-all-object-tutorial","status":"publish","type":"post","link":"https:\/\/www.rjmprogramming.com.au\/ITblog\/promise-all-object-tutorial\/","title":{"rendered":"Promise All Object Tutorial"},"content":{"rendered":"<div style=\"width: 230px\" class=\"wp-caption alignnone\"><a target=_blank href=\"https:\/\/www.rjmprogramming.com.au\/HTMLCSS\/promises_test.htm\"><img decoding=\"async\" style=\"border: 15px solid pink;\" alt=\"Promise All Object Tutorial\" src=\"http:\/\/www.rjmprogramming.com.au\/HTMLCSS\/promises_test_all.jpg\" title=\"Promise All Object Tutorial\"  style=\"float:left;\" \/><\/a><p class=\"wp-caption-text\">Promise All Object Tutorial<\/p><\/div>\n<p>Building on yesterday&#8217;s <a title='Promise Object Primer Tutorial' href='#popt'>Promise Object Primer Tutorial<\/a> start to the <a target=_blank title='Promise object' href='https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Promise'>Promise<\/a> object asynchronous processing research we are up to, we move on to try <a target=_blank title='Another useful David Walsh webpage, thanks' href='https:\/\/davidwalsh.name\/promises'>David Walsh<\/a>&#8216;s Promise.all syntax &#8230;<\/p>\n<p><code><br \/>\nPromise.all([promise1, promise2]).then(function(results) { \/\/ method takes an array of promises and fires one callback once they are all resolved<br \/>\n\t\/\/ Both promises resolved<br \/>\n})<br \/>\n.catch(function(error) {<br \/>\n\t\/\/ One or more promises was rejected<br \/>\n});<br \/>\n<\/code><\/p>\n<p> &#8230; and can&#8217;t say that the new <a target=_blank href=\"https:\/\/www.rjmprogramming.com.au\/HTMLCSS\/promises_test.htm\">live run<\/a> followed into &#8220;bowels&#8221; of the flow of the syntax, but worked with what was tried, elsewhere, and kicked off with Promise.all and before we forget, none of this works in Internet Explorer.   And what was that &#8220;try&#8221; all about?  &#8220;Catch&#8221; this.  We load the 2 web applications last night asynchronously in Ajax (invisibly) and then onto IFRAME src= loading (also invisible), but add into the mix a PHP Ajax call (all encased within the Promise.all method syntax) that sleeps for 75 seconds.  The idea here is that it is only after all three web applications finish their Ajax we display those IFRAME elements made invisible earlier.  The user sees the Dams in the USA, now, only well into its long read of data with the progress bar showing a good amount of progress.  Note here, we don&#8217;t pretend just client solutions can really sleep, as PHP represents a server side solution to this sleep issue we started talking about yesterday.<\/p>\n<p>Here is the amended HTML and Javascript you could call <a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/HTMLCSS\/promises_test.html-GETME\" title=\"promises_test.htm\">promises_test.htm<\/a> and here is <a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/PHP\/Geographicals\/diff.php?one=http:\/\/www.rjmprogramming.com.au\/HTMLCSS\/promises_test.html-GETME\" title=\"promises_test.htm\">what changed<\/a>.  Here is the very simple PHP <a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/HTMLCSS\/sleep.php_GETME\">sleep.php<\/a> &#8230; <\/p>\n<p><code><br \/>\n&lt;?php if (isset($_GET['n'])) { sleep($_GET['n']); echo \"&lt;p&gt;Slept for \" . $_GET['n'] . \" seconds.&lt;\/p&gt;\"; } else {   sleep(20); echo \"&lt;p&gt;Slept for 20 seconds.&lt;\/p&gt;\";   } ?&gt;<br \/>\n<\/code><\/p>\n<p>You can also see this play out at WordPress 4.1.1&#8217;s <a target=_blank  href='https:\/\/www.rjmprogramming.com.au\/ITblog\/promise-all-object-tutorial\/'>Promise All Object Tutorial<\/a>.<\/p>\n<hr>\n<p id='popt'>Previous relevant <a target=_blank title='Promise Object Primer Tutorial' href='https:\/\/www.rjmprogramming.com.au\/ITblog\/promise-object-primer-tutorial\/'>Promise Object Primer Tutorial<\/a> is shown below.<\/p>\n<div style=\"width: 230px\" class=\"wp-caption alignnone\"><a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/HTMLCSS\/promises_test.html\"><img decoding=\"async\" style=\"float:left; border: 15px solid pink;\" alt=\"Promise Object Primer Tutorial\" src=\"http:\/\/www.rjmprogramming.com.au\/HTMLCSS\/promises_test.jpg\" title=\"Promise Object Primer Tutorial\"   \/><\/a><p class=\"wp-caption-text\">Promise Object Primer Tutorial<\/p><\/div>\n<p>The <a target=_blank title='Promise object' href='https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Promise'>Promise<\/a> Javascript object opens up methods where you, as a web application client side programmer, can end up with a single var<font size=1>iable<\/font> approach to the management of asynchronous processes &#8230;<\/p>\n<p><img src='\/\/cdn.rawgit.com\/Vectaio\/a76330b025baf9bcdf07cb46e5a9ef9e\/raw\/26c4213a93dee1c39611dcd0ec12625811b20a26\/js-promise.svg' title='Diagram thanks to https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Promise'><\/img><\/p>\n<p> &#8230; but the use of this can&#8217;t hide the issue of what to do with client side logic to do nothing, what we think of as &#8220;sleep&#8221; in those &#8220;reject&#8221; parts of the diagram above.<\/p>\n<p>We&#8217;ve started our journey of understanding, with inspiration from <a target=_blank title='Useful link, thanks' href='http:\/\/www.html5rocks.com\/en\/tutorials\/es6\/promises\/#toc-promisifying-xmlhttprequest'>this useful link<a> (and <a target=_blank title='Another useful David Walsh webpage, thanks' href='https:\/\/davidwalsh.name\/promises'>this one<\/a>), thanks, of  <a target=_blank title='Promise object' href='https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Promise'>Promise<\/a> Javascript object by doing the <b>document.body onload event loaded<\/b> &#8230;<\/p>\n<p><code><br \/>\n&lt;script type='text\/javascript'&gt;<br \/>\nvar lleft=false, lright=false, cell=\"left\", damdone=false;<br \/>\nvar wis, adate;<br \/>\n<b><\/b><br \/>\n<b><\/b><br \/>\n\/\/ From Jake Archibald's Promises and Back:<br \/>\n\/\/ http:\/\/www.html5rocks.com\/en\/tutorials\/es6\/promises\/#toc-promisifying-xmlhttprequest<br \/>\n<b><\/b><br \/>\nfunction get(url) {<br \/>\n  \/\/ Return a new promise.<br \/>\n  return new Promise(function(resolve, reject) {<br \/>\n    \/\/ Do the usual XHR stuff<br \/>\n    var req = new XMLHttpRequest();<br \/>\n    req.open('GET', url);<br \/>\n<b><\/b><br \/>\n    req.onreadystatechange = function () {<br \/>\n    \/\/document.title='' + req.readyState + ' ' + req.status;<br \/>\n    if (req.readyState == 4 && req.status == 200) {<br \/>\n      console.log(req.responseText);<br \/>\n      document.getElementById(cell).innerHTML=req.response;<br \/>\n      if (req.response.indexOf('ighthous') != -1 || damdone) {<br \/>\n      adate=new Date();<br \/>\n      document.getElementById('middle').innerHTML+=adate.toLocaleString() + '&lt;br&gt;' + 'australian_lighthouses.php Ajax response&lt;br&gt;&lt;br&gt;';<br \/>\n      document.getElementById('hmiddle').innerHTML+=adate.toLocaleString() + '&lt;br&gt;' + 'australian_lighthouses.php Ajax response&lt;br&gt;&lt;br&gt;';<br \/>\n      adate=new Date();<br \/>\n      document.getElementById('hmiddle').innerHTML+=adate.toLocaleString() + '&lt;br&gt;' + 'australian_lighthouses.php IFRAME src&lt;br&gt;&lt;br&gt;';<br \/>\n      document.getElementById('h' + cell).innerHTML+='&lt;iframe onload=\"ldone(' + \"'australian_lighthouses.php'\" + ');\" style=\"width:800px;float:left;height:900px;\" src=\"\/\/www.rjmprogramming.com.au\/PHP\/australian_lighthouses.php#selsort\" title=\"Lighthouses in Australia\"&gt;&lt;\/iframe&gt;';<br \/>\n      } else {<br \/>\n      adate=new Date();<br \/>\n      document.getElementById('middle').innerHTML+=adate.toLocaleString() + '&lt;br&gt;' + 'dams_usa.html Ajax response&lt;br&gt;&lt;br&gt;';<br \/>\n      document.getElementById('hmiddle').innerHTML+=adate.toLocaleString() + '&lt;br&gt;' + 'dams_usa.html Ajax response&lt;br&gt;&lt;br&gt;';<br \/>\n      damdone=true;<br \/>\n      wis='' + eval(eval(window.getComputedStyle(document.getElementById(cell), null).getPropertyValue(\"width\").replace('px','')) - 0) + 'px';<br \/>\n      adate=new Date();<br \/>\n      if (document.getElementById('hmiddle').innerHTML == \"\") document.getElementById('hmiddle').innerHTML=document.getElementById('middle').innerHTML;<br \/>\n      document.getElementById('hmiddle').innerHTML+=adate.toLocaleString() + '&lt;br&gt;' + 'dams_usa.html IFRAME src&lt;br&gt;&lt;br&gt;';<br \/>\n      document.getElementById('h' + cell).innerHTML+='&lt;iframe onload=\"ldone(' + \"'dams_usa.html'\" + ');\" style=\"width:' + wis + ';float:left;height:900px;\" src=\"\/\/www.rjmprogramming.com.au\/HTMLCSS\/dams_usa.html\" title=\"Dams in the USA\"&gt;&lt;\/iframe&gt;';<br \/>\n      }<br \/>\n      cell=\"right\";<br \/>\n    }<br \/>\n    };<br \/>\n<b><\/b><br \/>\n    \/\/ Handle network errors<br \/>\n    req.onerror = function() {<br \/>\n      reject(Error(\"Network Error\"));<br \/>\n    };<br \/>\n<b><\/b><br \/>\n    \/\/ Make the request<br \/>\n    req.send();<br \/>\n  });<br \/>\n}<br \/>\n<b><\/b><br \/>\nfunction ldone(what) {<br \/>\n      adate=new Date();<br \/>\n      document.getElementById('hmiddle').innerHTML+=adate.toLocaleString() + '&lt;br&gt;' + what + ' IFRAME loaded&lt;br&gt;&lt;br&gt;';<br \/>\n}<br \/>\n<b><br \/>\nfunction ol() {<br \/>\nadate=new Date();<br \/>\ndocument.getElementById('middle').innerHTML+=adate.toLocaleString() + '&lt;br&gt;' + 'australian_lighthouses.php get()&lt;br&gt;&lt;br&gt;';<br \/>\ndocument.getElementById('hmiddle').innerHTML+=adate.toLocaleString() + '&lt;br&gt;' + 'australian_lighthouses.php get()&lt;br&gt;&lt;br&gt;';<br \/>\nget('\/\/www.rjmprogramming.com.au\/PHP\/australian_lighthouses.php').then(function(response) {<br \/>\n  console.log(\"Success!\", response);<br \/>\n}, function(error) {<br \/>\n  console.error(\"Failed!\", error);<br \/>\n});<br \/>\n<\/b><b><br \/>\nadate=new Date();<br \/>\ndocument.getElementById('middle').innerHTML+=adate.toLocaleString() + '&lt;br&gt;' + 'dams_usa.html get()&lt;br&gt;&lt;br&gt;';<br \/>\ndocument.getElementById('hmiddle').innerHTML+=adate.toLocaleString() + '&lt;br&gt;' + 'dams_usa.html get()&lt;br&gt;&lt;br&gt;';<br \/>\nget('\/\/www.rjmprogramming.com.au\/HTMLCSS\/dams_usa.html').then(function(response) {<br \/>\n  console.log(\"Success!\", response);<br \/>\n}, function(error) {<br \/>\n  console.error(\"Failed!\", error);<br \/>\n});<br \/>\n<\/b><b><br \/>\n}<br \/>\n<\/b><br \/>\n&lt;\/script&gt;<br \/>\n<\/code><\/p>\n<p> &#8230; asynchronous Ajax XMLHttpRequest() calls and follow this up with IFRAME src= calls, the difference being, the latter method loads any Javsacript and CSS styling in the &lt;head&gt;&lt;\/head&gt; tag whereas the former doesn&#8217;t.   We like the neatness of the syntax here, but still need those &#8220;reject&#8221; places to be able to effectively sleep to not overload your web browser with activity &#8230; and that&#8217;s where we&#8217;ll leave you with a <a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/HTMLCSS\/promises_test.html\" title=\"Click picture\">live run<a\/> HTML and Javascript <a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/HTMLCSS\/promises_test.html_GETME\">promises_test.html<\/a> source code link, thinking on that.<\/p>\n<p>If this was interesting you may be interested in <a title='Click here to see topics in which you might be interested' href='#d33606' onclick='var dv=document.getElementById(\"d33606\"); dv.innerHTML = \"&lt;iframe width=670 height=600 src=\" + \"https:\/\/www.rjmprogramming.com.au\/ITblog\/tag\/asynchronous\" + \"&gt;&lt;\/iframe&gt;\"; dv.style.display = \"block\";'>this<\/a> too.<\/p>\n<div id='d33606' style='display: none; border-left: 2px solid green; border-top: 2px solid green;'><\/div>\n<hr>\n<p>If this was interesting you may be interested in <a title='Click here to see topics in which you might be interested' href='#d33632' onclick='var dv=document.getElementById(\"d33632\"); dv.innerHTML = \"&lt;iframe width=670 height=600 src=\" + \"https:\/\/www.rjmprogramming.com.au\/ITblog\/tag\/sleep\" + \"&gt;&lt;\/iframe&gt;\"; dv.style.display = \"block\";'>this<\/a> too.<\/p>\n<div id='d33632' style='display: none; border-left: 2px solid green; border-top: 2px solid green;'><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Building on yesterday&#8217;s Promise Object Primer Tutorial start to the Promise object asynchronous processing research we are up to, we move on to try David Walsh&#8216;s Promise.all syntax &#8230; Promise.all([promise1, promise2]).then(function(results) { \/\/ method takes an array of promises and &hellip; <a href=\"https:\/\/www.rjmprogramming.com.au\/ITblog\/promise-all-object-tutorial\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,12,14,37],"tags":[69,1614,576,587,652,932,997,2357,2359,1319],"class_list":["post-33632","post","type-post","status-publish","format-standard","hentry","category-ajax","category-elearning","category-event-driven-programming","category-tutorials","tag-ajax","tag-asynchronous","tag-html","tag-iframe","tag-javascript","tag-php","tag-programming","tag-promise","tag-sleep","tag-tutorial"],"_links":{"self":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts\/33632"}],"collection":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/comments?post=33632"}],"version-history":[{"count":5,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts\/33632\/revisions"}],"predecessor-version":[{"id":33644,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts\/33632\/revisions\/33644"}],"wp:attachment":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/media?parent=33632"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/categories?post=33632"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/tags?post=33632"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}