这是我的代码,我总是得到"没有调用成功/错误"但我希望我的代码结束等待所有承诺得到解决或拒绝?我是对的吗?
SHORT示例我的代码如何显示(滚动一点,完整代码):
var promises = [];
for (var i = 0; i < 100; i++) {
promises[i] = new Parse.Promise();
if (i < 50) {
someAsyncParseFunctions() {
promises[i].resolve(i + "worked");
}
} else {
someAsyncParseFunctions() {
promises[i].reject(i + "doesnt work");
}
}
}
Parse.Promise.when(promises).then(function() {
response.success("All worked");
}, function(err) {
response.error(err.message);
});
完整代码:
Parse.Cloud.define("saveCard", function(request, response) {
var size = request.params.saveCardData.length;
var currentUser = JSON.stringify(request.params.currentUser).slice(3, -3);
var Card = Parse.Object.extend("Cards");
var Relation = Parse.Object.extend("Relations");
var promises = [];
function count(fbName, pUrl, index) {
var query = new Parse.Query(Card);
var promise = new Parse.Promise();
query.equalTo("Name", fbName);
var c = query.count({
success : function(count) {
promise.resolve({
name : fbName,
pictureUrl : pUrl,
count : count,
index : index
});
},
error : function(error) {
promise.reject({
index : index,
error : error.message
});
}
});
return promise;
};
function savePicture(object) {
var promise = new Parse.Promise();
Parse.Cloud.httpRequest({
url : object.pictureUrl,
success : function(httpResponse) {
var imageFile = new Parse.File(object.name.replace(" ", "_") + ".png", {
base64 : httpResponse.buffer.toString('base64', 0, httpResponse.buffer.length)
});
if ( typeof (imageFile) == "object") {
imageFile.save({
success : function(img) {
object.imageFile = img;
promise.resolve(object);
},
error : function(error) {
promise.reject({
index : object.index,
error : "error with creation of ImageFile"
});
}
});
} else {
promise.reject({
index : object.index,
error : "error with creation of ImageFile"
});
}
},
error : function(httpResponse) {
//HttpResponse doesn't work
promise.reject({
index : object.index,
error : httpResponse.status
});
}
});
return promise;
};
function saveCard(object) {
var promise = new Parse.Promise();
var newCard = new Card();
newCard.set('Name', object.name);
newCard.set('Image', object.imageFile);
newCard.save(null, {
success : function(newCard) {
object.newCard = newCard;
promise.resolve(object);
},
error : function(newCard, error) {
promise.reject({
index : object.index,
error : error.message
});
}
});
return promise;
};
function saveRelation(object) {
var promise = new Parse.Promise();
var relation = new Relation();
relation.set("cardID", object.name);
relation.set("userID", currentUser);
relation.set("Card", object.newCard.objectId);
relation.save(null, {
success : function(relation) {
object.relation = relation;
promise.resolve(object);
},
error : function(relation, error) {
promise.reject({
index : object.index,
error : error.message
});
}
});
return promise;
};
function checkRelation(object) {
var promise = new Parse.Promise();
var query = new Parse.Query(Relation);
query.equalTo('cardID', object.name);
query.equalTo('userID', currentUser);
query.find({
success : function(rel) {
if(typeof(rel.userID) == "undefined"){
object.relation = rel;
promise.resolve(object);
}else{
promise.resolve({
index: object.index,
error : "Die Relation existiert schon"
});
}
},
error : function(error) {
promise.resolve({
index: object.index,
error : error
});
}
});
return promise;
};
function getCardForRelation(object) {
var promise = new Parse.Promise();
var query = new Parse.Query(Card);
query.equalTo("Name", object.name);
query.find({
success : function(card) {
object.newCard = card;
promise.resolve(object);
},
error : function(error) {
promise.reject({
index : object.index,
error : error.message
});
}
});
return promise;
};
for (var i = 0; i < size; i++) {
var params = request.params.saveCardData.pop();
var pictureUrl = params.pop();
var name = params.pop();
promises[i] = new Parse.Promise();
count(name, pictureUrl, i).then(function(object) {
if ( typeof (object.name) == "string") {
if (object.count == 0) {
//Card doesnt exists
savePicture(object).then(function(object) {
if ( typeof (object.name) == "string") {
//Bild erstellt
saveCard(object).then(function(object) {
if ( typeof (object.name) == "string") {
//Image file and Card saved
saveRelation(object).then(function(object) {
if(typeof(object.name) == "string") {
//Relation erstellt
console.log(object.relation.get("cardID") + " erstellt! In Relation zu: " + object.relation.get("userID"));
promises[object.index].resolve(object.relation.get("cardID") + " erstellt! In Relation zu: " + object.relation.get("userID"));
}else {
//Relation nicht erstellt
promises[object.index].reject(object.error);
}
});
} else {
//Image File and Card not created
promises[object.index].reject(object.error);
}
});
} else {
//Fehler beim Bild erstellen
promises[object.index].reject(object.error);
}
});
} else {
//Card already exists
checkRelation(object).then(function(object) {
if ( typeof (object.name) == "string") {
getCardForRelation(object).then(function(object) {
if ( typeof (object.name) == "string") {
//Karte gefunden
saveRelation(object).then(function(object) {
if(typeof(object.name) == "string") {
//Relation erstellt
console.log(object.relation.get("cardID") + " erstellt! In Relation zu: " + object.relation.get("userID"));
promises[object.index].resolve(object.relation.get("cardID") + " erstellt! In Relation zu: " + object.relation.get("userID"));
}else {
//Relation nicht erstellt
promises[object.index].reject(object.error);
}
});
} else {
//Karte nicht gefunden
promises[object.index].reject(object.error);
}
});
} else {
promises[object.index].reject("Relation existiert doch schon!");
}
});
}
} else {
promises[object.index].reject(object.error);
}
});
}
Parse.Promise.when(promises).then(function() {
response.success("Worked fine");
}, function(err) {
response.error(err);
});
});
如果你们中的一些人可以帮我解决这个问题会很好!我真的很努力!也许这只是我身边的一个小错误!