Solved

How to check the value of a returned promise

Posted on 2016-09-21
2
25 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 54

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Introduction Chart.js, used properly, can visually add a difference to your charting applications. It engages your visitors and allows them to interact with data they otherwise wouldn't be able to without expensive and complicated systems. For this…
Introduction If you're like most people, you have occasionally made a typographical error when you're entering information into an online form.  And to your consternation, the browser remembers the error, and offers to autocomplete your future entr…
The viewer will learn how to dynamically set the form action using jQuery.
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)

773 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