{"id":33606,"date":"2017-10-10T03:01:04","date_gmt":"2017-10-09T17:01:04","guid":{"rendered":"http:\/\/www.rjmprogramming.com.au\/ITblog\/?p=33606"},"modified":"2017-10-11T19:39:23","modified_gmt":"2017-10-11T09:39:23","slug":"promise-object-primer-tutorial","status":"publish","type":"post","link":"https:\/\/www.rjmprogramming.com.au\/ITblog\/promise-object-primer-tutorial\/","title":{"rendered":"Promise Object Primer Tutorial"},"content":{"rendered":"<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","protected":false},"excerpt":{"rendered":"<p>The Promise Javascript object opens up methods where you, as a web application client side programmer, can end up with a single variable approach to the management of asynchronous processes &#8230; &#8230; but the use of this can&#8217;t hide the &hellip; <a href=\"https:\/\/www.rjmprogramming.com.au\/ITblog\/promise-object-primer-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,997,2357,1319],"class_list":["post-33606","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-programming","tag-promise","tag-tutorial"],"_links":{"self":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts\/33606"}],"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=33606"}],"version-history":[{"count":13,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts\/33606\/revisions"}],"predecessor-version":[{"id":33623,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts\/33606\/revisions\/33623"}],"wp:attachment":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/media?parent=33606"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/categories?post=33606"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/tags?post=33606"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}