Solved

How to check the value of a returned promise

Posted on 2016-09-21
2
17 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 51

Accepted Solution

by:
Julian Hansen earned 500 total points (awarded by participants)
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer …
Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
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)

762 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

11 Experts available now in Live!

Get 1:1 Help Now