INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Understanding promises

Understanding promises

(OP)
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="http://www.w3.org/1999/xhtml">
<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

RE: Understanding promises

AFAIK, Javascript is single-threaded by default, so asynchronous requests can be queued and dispatched in a no-predictable way

Cheers,
Dian

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close