解析 Cloud Code Promise 未正确返回
Parse Cloud Code Promise not returning correctly
我以为我已经弄清楚了 Promises 的事情,但是这个从查询中获取所有结果而不考虑 1000 项限制的函数无法正常工作。谁能向我解释我哪里出错了?谢谢!
此云作业在我的 main.js 文件中:
//in main.js
Parse.Cloud.job("testQuery", function(request, status) {
var theClass = Parse.Object.extend("SomeClass");
var theQuery = new Parse.Query(theClass);
console.log("Here we go...");
queryHelper.getAllResultsForQuery(theQuery, console).then( function(result) {
//This code is called before getAllResultsForQuery finishes
console.log("Finished the search!");
status.success("yay");
}, function(error) {
console.log("Fail " + error);
status.error("failed!");
});
});
而这个函数在queryHelper.js:
//in queryHelper.js
exports.getAllResultsForQuery = function(query, console) {
var resultArray = [];
var limit = 1000;
var sendQuery = function(skip) {
if (skip) {
query.greaterThan("createdAt", skip);
}
query.limit(limit);
query.ascending("createdAt");
query.find().then(function (newResults) {
receivedResults(newResults);
}, function (error) {
return Parse.Promise.error(new Error("query failed after " + resultArray.length + " results, error:" + error));
});
};
var receivedResults = function(received) {
resultArray = resultArray.concat(received);
console.log("Got " + received.length + " objects, now at " + resultArray.length);
if (received.length == limit) {
sendQuery(received[received.length-1].createdAt);
} else {
console.log("returning from getAllResults...");
return resultArray;
}
};
sendQuery();
};
当我 运行 代码时,queryHelper.getAllResultsForQuery 函数似乎 运行 就好了,但是 Promise 似乎在该函数完成之前触发,所以控制台输出看起来像这个:
"Here we go…";
"Finished the search!";
"Got 1000 objects, now at 1000";
"Got 1000 objects, now at 2000";
"Got 1000 objects, now at 3000";
"Got 245 objects, now at 3245";
"returning from getAllResults…";
我是不是在某个地方犯了新手错误?
getAllResultsForQuery 应该 return 直接承诺。我标记添加 returns.
exports.getAllResultsForQuery = function(query, console) {
var resultArray = [];
var limit = 1000;
var sendQuery = function(skip) {
if (skip) {
query.greaterThan("createdAt", skip);
}
query.limit(limit);
query.ascending("createdAt");
return query.find().then(function (newResults) { //return added here
return receivedResults(newResults); //return added here
}, function (error) {
return Parse.Promise.error(new Error("query failed after " +
resultArray.length + " results, error:" + error));
});
};
var receivedResults = function(received) {
resultArray = resultArray.concat(received);
console.log("Got " + received.length + " objects, now at " +
resultArray.length);
if (received.length == limit) {
return sendQuery(received[received.length-1].createdAt).then(function (received) {
//added code here
resultArray.concat(results);
return resultArray;
}); //return added here
} else {
console.log("returning from getAllResults...");
return resultArray;
}
};
return sendQuery(); //return added here
};
我以为我已经弄清楚了 Promises 的事情,但是这个从查询中获取所有结果而不考虑 1000 项限制的函数无法正常工作。谁能向我解释我哪里出错了?谢谢!
此云作业在我的 main.js 文件中:
//in main.js
Parse.Cloud.job("testQuery", function(request, status) {
var theClass = Parse.Object.extend("SomeClass");
var theQuery = new Parse.Query(theClass);
console.log("Here we go...");
queryHelper.getAllResultsForQuery(theQuery, console).then( function(result) {
//This code is called before getAllResultsForQuery finishes
console.log("Finished the search!");
status.success("yay");
}, function(error) {
console.log("Fail " + error);
status.error("failed!");
});
});
而这个函数在queryHelper.js:
//in queryHelper.js
exports.getAllResultsForQuery = function(query, console) {
var resultArray = [];
var limit = 1000;
var sendQuery = function(skip) {
if (skip) {
query.greaterThan("createdAt", skip);
}
query.limit(limit);
query.ascending("createdAt");
query.find().then(function (newResults) {
receivedResults(newResults);
}, function (error) {
return Parse.Promise.error(new Error("query failed after " + resultArray.length + " results, error:" + error));
});
};
var receivedResults = function(received) {
resultArray = resultArray.concat(received);
console.log("Got " + received.length + " objects, now at " + resultArray.length);
if (received.length == limit) {
sendQuery(received[received.length-1].createdAt);
} else {
console.log("returning from getAllResults...");
return resultArray;
}
};
sendQuery();
};
当我 运行 代码时,queryHelper.getAllResultsForQuery 函数似乎 运行 就好了,但是 Promise 似乎在该函数完成之前触发,所以控制台输出看起来像这个:
"Here we go…";
"Finished the search!";
"Got 1000 objects, now at 1000";
"Got 1000 objects, now at 2000";
"Got 1000 objects, now at 3000";
"Got 245 objects, now at 3245";
"returning from getAllResults…";
我是不是在某个地方犯了新手错误?
getAllResultsForQuery 应该 return 直接承诺。我标记添加 returns.
exports.getAllResultsForQuery = function(query, console) {
var resultArray = [];
var limit = 1000;
var sendQuery = function(skip) {
if (skip) {
query.greaterThan("createdAt", skip);
}
query.limit(limit);
query.ascending("createdAt");
return query.find().then(function (newResults) { //return added here
return receivedResults(newResults); //return added here
}, function (error) {
return Parse.Promise.error(new Error("query failed after " +
resultArray.length + " results, error:" + error));
});
};
var receivedResults = function(received) {
resultArray = resultArray.concat(received);
console.log("Got " + received.length + " objects, now at " +
resultArray.length);
if (received.length == limit) {
return sendQuery(received[received.length-1].createdAt).then(function (received) {
//added code here
resultArray.concat(results);
return resultArray;
}); //return added here
} else {
console.log("returning from getAllResults...");
return resultArray;
}
};
return sendQuery(); //return added here
};