WitVault WitVault - 3 years ago 142
Ajax Question

Mixing jquery promise with Parse promises

I am using jquery to send an ajax request to perform certain tasks and based on the result of that jquery GET response I am performing some action on Parse Js library.


I have created a method like this to send the request.

sendRequest(URL, userId){
return $.ajax({
url: URL + userId,
type: 'GET',
}).fail((responseData) => {
if (responseData.responseCode) {

I am using it like this -

sendRequest(URL, userId)
(data) => {
// Example
// Get some value from data and save it in Parse object
var GameScore = Parse.Object.extend("GameScore");
var gameScore = new GameScore();

gameScore.set("score", 1337);
gameScore.set("playerName", "Sean Plott");
gameScore.set("cheatMode", false);

return gameScore.save();
(changedGameObj) => {
// At this point receiving a parse promise which is not resolved yet.
(error) => {

I know that I am mixing jquery promise and Parse promise but don't know the solution of how to get resolved parse promises because jquery promises gets resolved earlier.

I am quite new to promise in js and please point me where I am wrong.

Answer Source

From what I can see your promise chain is this (removing functions, etc):

$.ajax().fail().then().then(success(), error());

You can see the documentation for jQuery's deferred for more details.

There's two things to try:

1) Convert jQuery deferred to a real promise. I'd do this because I don't care to learn jQuery's deferred. You may need to dig in here so...


2) Chain your Parse promises together in the same then():

sendRequest(URL, userId)
.then( (data) => {
  // ...
  return gameScore.save()      // this is a promise
  .then( (changedGameObj) => { // success
  }, (error) => {              // error
  });                          // this whole promise is returned to the deferred.
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download