与承诺一起工作,做一个保存
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);
}
这样写承诺太糟糕了。你想要使用promise的全部原因是,这样你就可以链式回调了。在你的例子中,这是两个世界中最糟糕的,承诺的复杂性,但你仍然嵌套。
第二个问题是,您确实需要放置一个最后的错误处理程序。现在发出的任何错误都可能消失。总是以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);
}
相关文章:
- 如何将多个画布保存为一个图像
- Javascript/jQuery压缩一个图像正确的评论帖子,并使用Ajax Laravel 5.2保存到控制器中
- 如何使用一个按钮编辑和保存使用Javascript
- 将多个重叠的透明图像保存为一个图像
- 保存两个模型(属于第三个模型)和一个提交
- Angular/Ionic:将所有文本保存在一个单独的文件中
- html表单,它有文本和表,现在我想在一个简单的文本文件中保存和检索数据,如何实现它
- 在我的浏览器中用类似ctrl+s的PhantomJS保存一个网站
- 如何上传一个XML对象并用Perl将其保存在服务器上
- 使用javascript/jquery从现有数组中创建一个新数组,该数组保存项目存在的次数
- 有没有一个javascript库能够修改和保存图像
- 如何强制用户从下拉列表和按钮中选择一个值以启用保存按钮
- 将多个画布保存为一个图像(使网站像PicFrame一样)
- Parse.com在循环中保存对象,只保存最后一个对象
- 如何创建一个保存var值的函数
- 如何从会话的一个保存点跳到另一个保存点将
- 如何传递一个保存的Javascript变量在Chrome扩展到谷歌应用引擎应用程序
- 如何为多个文本字段只有一个保存按钮
- 是什么导致一个保存的网页呈现不同于它的在线URL对应
- 与承诺一起工作,做一个保存