-3

There is a funcion returning a promise:

req.postP = function () {

    var $promise = $.ajax({
        url: self._url(url),
        type: "POST",
        data: JSON.stringify(data),
        headers: headers,
        contentType: self.JSON_CONTENT_TYPE,
        dataType: "json"
    });

    return new Promise( (resolve, reject) => {
        return $promise.then(resolve, reject);
    } );

}

And another function that calls the first function:

req
    .postP(POST_REGISTER, userData)
    .then((data) => {
        // is never ecexuted
    }) 
    .catch((err) => {
        console.log(typeof err.then === "function"); // true
    });

The then() function is never executed.

It seems that the promise is inside the catch function as err since err.then is a function.

The req.postP() function is called by other function as well and works in those cases.

Maurice Wipf
  • 647
  • 2
  • 7
  • 13

1 Answers1

0

The casting to a standard promise, was not the problem.

The problem was the dataType. According to the jQuery docs:

The type of data that you're expecting back from the server.

Since the server does not return any data for this request on success - but jQuery expected data - the promise was always rejected.

Related: jQuery returning "parsererror" for ajax request

Maurice Wipf
  • 647
  • 2
  • 7
  • 13