javascript - Working with promises, doing a save -


i have following cloud function returning immediately, promise full-filled without doing job. can 1 see why?

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

if use code :

myfunction(newltr).then (function() {  console.log("myfunction full-filled.");  } 

i can see messages "entered myfunction." , "myfunction full-filled." in logs. never see "1)newvalarray:..." neither see "2)newvalarray:..." have checked passed parameter has not been processed expected.

if replace myfunction following version, doesn't make difference:

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

that's terrible way write promises. whole reason want use promises in first place, can chain callbacks. in example it's worst of both worlds, complexity of promises you're still nesting.

the second issue need place final error handler. errors emitted right might disappear. always end catch.

i rewrote first function correctly promises, can't guarantee if there's not else wrong. helps along way:

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) {           // added third clause. returning          // parse.promise.as() presumably wanted          // that. before clause got discarded,          // change result of parse.promise.as() thrown in          // 'result' argument in function.       }).catch(function(err) {           // if error thrown in part of change,          // bubble final catch statement.          // err! log or whatever ;)       })      return parse.promise.when(classpromise); } 

Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -