Link to home
Start Free TrialLog in
Avatar of Rohit Bajaj
Rohit BajajFlag for India

asked on

How to check the value of a returned promise

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
ASKER CERTIFIED SOLUTION
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial