1

I have been looking at all sorts of discussions concerning how to use promises, and I am not getting anything to work.

I keep getting an error "Cannot read property then of undefined."

Is the "then," "done," etc. built-in to Javascript? Or do they require me to include some other external script?

Here is my most recent attempt to experiment with 2 simple dialogs (confirm and reject are both simple dialogs):

var confirmWithPromise = Confirm(); 
var reject = confirmWithPromise.then(Reject("This record cannot be deleted."));

If I could just get started with the simplest way of getting my example to work, I think I could take it from there.

Thanks.

Update: Here is my Confirm() - which is not returning a promise. I don't fully understand how to implement the return of it:

 function Confirm() {
    var buttons = [
{
    text: "Yes",
    //icons: {
    //    primary: "ui-icon-heart"
    //},
    click: function () {
        $(this).dialog("close");
        callback(true);
    }

    // Uncommenting the following line would hide the text,
    // resulting in the label being used as a tooltip
    //showText: false
},
{
    text: "No",
    //icons: {
    //    primary: "ui-icon-heart"
    //},
    click: function () {
        $(this).dialog("close");
        callback(false);
    }

    // Uncommenting the following line would hide the text,
    // resulting in the label being used as a tooltip
    //showText: false
}
    ];

    showDialog("Confirm", "Are you sure?  Once the record is deleted, it cannot be recovered.", buttons);

}
  • 1
    Is `Confirm` returning a Promise? `function Confirm() { return new Promise(...); }` – Mike Cluck May 31 '16 at 16:08
  • 2
    The error seems pretty clear: `Confirm` doesn't return anything. *"Is the "then," "done," etc. built-in to Javascript? "* Promises are pare of JavaScript, yes. – Felix Kling May 31 '16 at 16:12
  • If you provided the code for Confirm we could see what it is returning – Simon H May 31 '16 at 16:13

2 Answers2

4

Is the "then," "done," etc. built-in to Javascript?

Yes. They are methods of Promise objects which are defined by the ES6 specification.

I keep getting an error "Cannot read property then of undefined."

You can only use them on promise objects. That error message indicates that the return value of Confirm (which isn't a JavaScript built-in, although confirm (with a c not a C) is provided as a web API by browsers) is undefined and not a promise object.

You would need to edit Confirm so that it returned a promise.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
-1

Promises are part of the latest spec, but not all browsers implement them - IE11 notably. You can overcome this by either using what's called a "promise polyfill' or use the jQuery implementation of promises that's implemented via their "deferred" construct - it isn't an implementation of Promises per se, but it implements a very similar general concept.

In your case, your 'Confirm()' method must return a 'promise' object. In jQuery, Confirm would likely look like:

function Confirm(){
   var deferred = $.Deferred();

   ///do something interesting and asynchronous

   return deferred.promise();
}

Once the promise is returned, the ".then" elements are then available for processing the appropriate callbacks when deferred.reject() or deferred.resolve() is called.

David W
  • 10,062
  • 34
  • 60
  • 2
    Suggesting the jQuery implementation is a horrible idea. –  May 31 '16 at 16:16
  • Because? If his environment doesn't natively support the Promise implementation, he'll need to go some other route. Should he avoid the promise altogether merely because there's a downside to the jQuery implementation? I understand the opinion that jQuery's implementation isn't ideal, but if he has no native alternative.... – David W May 31 '16 at 16:17
  • 1
    @DavidW 1) OP didn't mention any browser incompatibilities so I'm not sure why you're suggesting an alternative anyway. 2) They should use a *real* Promise implementation such as [bluebird](https://github.com/petkaantonov/bluebird) or [es6-promise](https://github.com/stefanpenner/es6-promise). – Mike Cluck May 31 '16 at 16:23
  • As we didn't initially know the content of the OP's Confirm dialog, it wasn't unreasonable to infer the possibility they were trying to use Promises in an environment that doesn't implement them, hence the suggestion for the alternative. The intent was to help. Man, the rush to hostility here is getting so frustrating... – David W May 31 '16 at 16:25
  • @DavidW Suggesting an alternative is fine, but as Mike C points out, this is a really bad alternative to suggest. – JLRishe May 31 '16 at 16:56
  • @JLRishe Fine, but if the OP (for whatever reason) can't access either source, but has jQuery in hand, it's at least *some* alternative. Many organizations restrict access to third-party sources no matter how much the "real world" prefers them. – David W May 31 '16 at 17:03