• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 64
  • Last Modified:

How to check the value of a returned promise

I have the following html with promises and defered in script tag :
<!DOCTYPE html>
    <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>
        body {
            background-color: linen;
        h1 {
            color: maroon;
            margin-left: 40px;
  <div id="note">note</div>
  <button id="button">toggle</button>
     $('#button').on('click', function(){

    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()) {
            // 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));
    return dfd.promise;

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


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 ??

Rohit Bajaj
Rohit Bajaj
1 Solution
Julian HansenCommented:
Take a look at this small sample
$(function() {
  var dfd = $.Deferred();
  setTimeout(function() {
  } , 5000);

  dfd.then(function(val) {
    console.log('Resolved with value : ' + val);

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.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now