Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Understanding promises

Status
Not open for further replies.

tfstom

Programmer
Sep 28, 2002
190
US
I am trying to understand how to use promises. I am having a little problem with pouchDB so I needed to go back and research how promises worked.

I have the following sample page I created with three promises and three status lines (two between promises and one after the promises).

Code:
<!DOCTYPE html>
<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml">[/URL]
<head>
    <title></title>
</head>
<body>
   <script>

      var jsonPromiseBad = new Promise(function (resolve, reject) {
         // JSON.parse throws an error if you feed it some
         // invalid JSON, so this implicitly rejects:
         resolve(JSON.parse("This ain't JSON<br><br>"));
      });

      var jsonPromiseGood = new Promise(function(resolve, reject) {
         // JSON.parse throws an error if you feed it some
         // invalid JSON, so this implicitly rejects:
         resolve(JSON.parse('[1, 2, 3, 4 ]'));
      });

      var promiseFinished = new Promise(function (resolve, reject) {
         // JSON.parse throws an error if you feed it some
         // invalid JSON, so this implicitly rejects:
         resolve('In promise "promiseFinished"!<br><br>');
      });

      jsonPromiseBad.then(function(data) {
         // This never happens:
         document.write("It worked! - Promise 1 - ", data + "<br><br>");
      }).catch(function(err) {
         // Instead, this happens:
         document.write("It failed! - Promise 1 - ", err + "<br><br>");
      });

      document.write("Write between promises 1 and 2!<br><br><br>");

      jsonPromiseGood.then(function (data) {
         // This never happens:
         document.write("It worked! - Promise 2 - ", data + "<br><br>");
      }).catch(function (err) {
         // Instead, this happens:
         document.write("It failed! - Promise 2 - ", err + "<br><br>");
      });

      document.write("Write between promises 2 and 3!<br><br><br>");

      promiseFinished.then(function (data) {
         // This never happens:
         document.write("It worked! - Promise 3 - ", data + "<br><br>");
      }).catch(function (err) {
         // Instead, this happens:
         document.write("It failed! - Promise 3 - ", err + "<br><br>");
      });

      document.write("Write after promise 3!<br><br><br>");

   </script>
</body>
</html>

Results as they appear on the page:

Code:
Write between promises 1 and 2!
Write between promises 2 and 3!
Write after promise 3!
It worked! - Promise 2 - 1,2,3,4
It worked! - Promise 3 - In promise "promiseFinished"!
It failed! - Promise 1 - SyntaxError: Unexpected token T in JSON at position 0

I think I understand why the straight document.writes happened first - they are quicker than a promise a JSON.parse.

I would have thought that #3 would have finished before #2 since it just has a straight document.write and #2 and 3 are promises and JSON.parse's. Also, why would #2 finish before #1?

Thanks,

Tom
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top