Solved

How to check the value of a returned promise

Posted on 2016-09-21
2
20 Views
Last Modified: 2016-10-10
HI,
I have the following html with promises and defered in script tag :
<!DOCTYPE html>
<html>
  <head>
    <title>Create Note</title>
    <meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
    <style>
        body {
            background-color: linen;
        }
        h1 {
            color: maroon;
            margin-left: 40px;
        }
    </style>
   
  </head>
  <body>
  <div id="note">note</div>
  <button id="button">toggle</button>
</body>
 <script>
     $('#button').on('click', function(){
         $('#note').toggle();

     });
    function poll(fn, timeout, interval) {
    var dfd = $.Deferred();
    var endTime = Number(new Date()) + (timeout || 2000);
    interval = interval || 100;

    (function p() {
            // If the condition is met, we're done! 
            if(fn()) {
                dfd.resolve();
            }
            // If the condition isn't met but the timeout hasn't elapsed, go again
            else if (Number(new Date()) < endTime) {
                setTimeout(p, interval);
            }
            // Didn't match and too much time, reject!
            else {
                dfd.reject(new Error('timed out for ' + fn + ': ' + arguments));
            }
    })();
    console.log('finished');
    return dfd.promise;
}

// Usage:  ensure element is visible
promise = poll(function() {
  return document.getElementById('note').offsetWidth > 0;
}, 2000, 150);

    </script>
</html>

Open in new window


I have got the promise object but how do i check what value it has  or whether the defered object was resolve or rejected.... Also i see there is no values set to it.
Its only doing dfd.resolve and reject do these set some value in the promise ??

Thanks
0
Comment
Question by:Rohit Bajaj
2 Comments
 
LVL 53

Accepted Solution

by:
Julian Hansen earned 500 total points (awarded by participants)
ID: 41808633
Take a look at this small sample
<script>
$(function() {
  var dfd = $.Deferred();
  setTimeout(function() {
    dfd.resolve(100);
  } , 5000);

  dfd.then(function(val) {
    console.log('Resolved with value : ' + val);
  });
});
</script>

Open in new window

In this code we create a promise.
We start a timeout to simulate an Async operation.
We attach a then handler to the promise to do something with the value when the promise resolves.
When our timeout happens we resolve the promise with a value - this value is passed to the then callback where we use the value.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

PROBLEM: The other day I was working on adding an ajax request to a webpage that already had a dialog box on the page.  The dialog box was using relative positioning to be positioned next to a form field I had on the page.  Everything was working…
How to build a simple, quick and effective accordion menu using just 15 lines of jQuery and 2 css classes
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

27 Experts available now in Live!

Get 1:1 Help Now