简体   繁体   中英

Uncaught TypeError: undefined is not a function

Below javascript snippet is giving error on .done statement. Error is "Uncaught TypeError: undefined is not a function " Since we can use done with promise any idea why this error is coming?

var promise = new Promise(function(resolve,reject)
                {

                    if(true)
                    {
                        console.log("1");

                    }
                    else
                        console.log("2");


                });
    promise.then( function(data1) {

                     if(true)
                    {
                        console.log("3");
                    }

                    else
                       console.log("4");
                })
                .then( function(data2) {

                     if(true)
                    {
                        console.log("5");
                    }
                    else
                        console.log("6");

                })
               .done( 
                    function(response) {
                       console.log("done")
                    });
                .fail(
                    function() {
                        console.log("fail");
                    });

There is no such method done nor fail in the Promise API, only then and catch at the object level: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise#Methods

Promise.prototype.then(onFulfilled, onRejected)

Appends fulfillment and rejection handlers to the promise, and returns a new promise resolving to the return value of the called handler.

Promise.prototype.catch(onRejected)

Appends a rejection handler callback to the promise, and returns a new promise resolving to the return value of the callback if it is called, or to its original fulfillment value if the promise is instead fulfilled.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM