Solved

How to check the value of a returned promise

Posted on 2016-09-21
2
33 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 56

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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…
This article discusses how to create an extensible mechanism for linked drop downs.
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)

756 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