与承诺一起工作,做一个储蓄
Working with promises, doing a save
我有以下云函数立即返回,承诺已满,但没有完成它的工作。谁能看出原因吗?
function myFunction(newValArray)
{
console.log("Entered myFunction.");
var query,classPromise;
query = new Parse.Query("myClass");
classPromise = (query.find().then
(function(result) {
result[0].set("myField", result[0].get("myField")+1);
result[0].save(null,{}).then
(function() {
console.log("1)newValArray:"+newValArray.length.toString());
newValArray.push(result[0].get("myField"));
console.log("2)newValArray:"+newValArray.length.toString());
return Parse.Promise.as();
});
}));
return Parse.Promise.when(classPromise);
}
如果我将此代码用于:
myFunction(newLTR).then
(function() {
console.log("myFunction FULL-FILLED.");
}
我可以在日志中看到消息 "Entered myFunction."
和 "myFunction FULL-FILLED."
。
但是我从来没有看到 "1)newValArray:..."
我也没有看到 "2)newValArray:..."
我还检查了传递的参数没有按预期处理。
如果我将 myFunction 替换为以下版本,则没有任何区别:
function myFunction(newValArray)
{
console.log("Entered myFunction.");
var query,classPromise;
query = new Parse.Query("Configuration");
classPromise = (query.find().then
(function(result) {
result[0].set("myField", result[0].get("myField")+1);
result[0].save(null,{
success:function(configRcd) {
console.log("1)newValArray:"+newValArray.length.toString());
newValArray.push(configRcd.get("myField"));
console.log("2)newValArray:"+newValArray.length.toString());
return Parse.Promise.as();
},
error:function(error) {
console.log("Something went wrong in incrementLastTouchReference.");
}});
}));
return Parse.Promise.when(classPromise);
}
这种写承诺的方式很糟糕。您首先要使用 promises 的全部原因是您可以链接回调。在你的例子中,这是两个世界中最糟糕的,承诺的复杂性,但你仍在嵌套。
第二个问题是您确实需要放置一个最终错误处理程序。现在发出的任何错误都可能会消失。 总是以catch
结束。
我将你的第一个函数重写为 正确 做承诺,但我不能保证是否没有其他问题。希望它对您有所帮助:
function myFunction(newValArray)
{
console.log("Entered myFunction.");
var query,classPromise;
query = new Parse.Query("myClass");
classPromise = query.find()
.then(function(result) {
result[0].set("myField", result[0].get("myField")+1);
return result[0].save(null,{});
}).then(function() {
console.log("1)newValArray:" + newValArray.length.toString());
newValArray.push(result[0].get("myField"));
console.log("2)newValArray:"+newValArray.length.toString());
return Parse.Promise.as();
}).then(function(result) {
// I added this third then clause. You were returning
// Parse.Promise.as() so presumably you wanted to do something
// with that. Before this then clause it got discarded, with my
// change the result of Parse.Promise.as() is thrown in the
// 'result' argument in this function.
}).catch(function(err) {
// If an error is thrown in any part of the change, it will
// bubble to this final catch statement.
// Do something with err! Log it or whatever ;)
})
return Parse.Promise.when(classPromise);
}
我有以下云函数立即返回,承诺已满,但没有完成它的工作。谁能看出原因吗?
function myFunction(newValArray)
{
console.log("Entered myFunction.");
var query,classPromise;
query = new Parse.Query("myClass");
classPromise = (query.find().then
(function(result) {
result[0].set("myField", result[0].get("myField")+1);
result[0].save(null,{}).then
(function() {
console.log("1)newValArray:"+newValArray.length.toString());
newValArray.push(result[0].get("myField"));
console.log("2)newValArray:"+newValArray.length.toString());
return Parse.Promise.as();
});
}));
return Parse.Promise.when(classPromise);
}
如果我将此代码用于:
myFunction(newLTR).then
(function() {
console.log("myFunction FULL-FILLED.");
}
我可以在日志中看到消息 "Entered myFunction."
和 "myFunction FULL-FILLED."
。
但是我从来没有看到 "1)newValArray:..."
我也没有看到 "2)newValArray:..."
我还检查了传递的参数没有按预期处理。
如果我将 myFunction 替换为以下版本,则没有任何区别:
function myFunction(newValArray)
{
console.log("Entered myFunction.");
var query,classPromise;
query = new Parse.Query("Configuration");
classPromise = (query.find().then
(function(result) {
result[0].set("myField", result[0].get("myField")+1);
result[0].save(null,{
success:function(configRcd) {
console.log("1)newValArray:"+newValArray.length.toString());
newValArray.push(configRcd.get("myField"));
console.log("2)newValArray:"+newValArray.length.toString());
return Parse.Promise.as();
},
error:function(error) {
console.log("Something went wrong in incrementLastTouchReference.");
}});
}));
return Parse.Promise.when(classPromise);
}
这种写承诺的方式很糟糕。您首先要使用 promises 的全部原因是您可以链接回调。在你的例子中,这是两个世界中最糟糕的,承诺的复杂性,但你仍在嵌套。
第二个问题是您确实需要放置一个最终错误处理程序。现在发出的任何错误都可能会消失。 总是以catch
结束。
我将你的第一个函数重写为 正确 做承诺,但我不能保证是否没有其他问题。希望它对您有所帮助:
function myFunction(newValArray)
{
console.log("Entered myFunction.");
var query,classPromise;
query = new Parse.Query("myClass");
classPromise = query.find()
.then(function(result) {
result[0].set("myField", result[0].get("myField")+1);
return result[0].save(null,{});
}).then(function() {
console.log("1)newValArray:" + newValArray.length.toString());
newValArray.push(result[0].get("myField"));
console.log("2)newValArray:"+newValArray.length.toString());
return Parse.Promise.as();
}).then(function(result) {
// I added this third then clause. You were returning
// Parse.Promise.as() so presumably you wanted to do something
// with that. Before this then clause it got discarded, with my
// change the result of Parse.Promise.as() is thrown in the
// 'result' argument in this function.
}).catch(function(err) {
// If an error is thrown in any part of the change, it will
// bubble to this final catch statement.
// Do something with err! Log it or whatever ;)
})
return Parse.Promise.when(classPromise);
}