与承诺一起工作,做一个保存

Working with promises, doing a save

本文关键字:一个 保存 承诺 一起 工作      更新时间:2023-09-26

我有下面的云函数立即返回,承诺完全填充,但没有做它的工作。有人知道为什么吗?

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);
}