firebase - Need help understanding this promise and handling the error -
i saving data firebase database inside polymer element. works fine. person who's new promises need understand promise.resolved() means here @ end of method. isn't promise going through before when .then used? doing? looked around can't find example of resolved() no value.
and how can change have more familiar structure below:
.then(function(snapshot) { // promise "fulfilled" (it succeeded). }, function(error) { // promise rejected. }); here's block , promise.resolved() taken documentation:
savedata : function() { this.$.document.data = this.$.message.value; this.$.document.save("/parent", "child").then(function() { console.log('sent event!!!!!!'); this.$.document.reset(); }.bind(this)); return promise.resolve(); },
first need understand basics of promises.
lets start basics -
a newly created es6 promise in 1 of following states:
- resolved
- rejected
- pending --> waiting either resolved or rejected
lets create sample promise
var promise = new promise(function(fulfill, reject) { // stuff , either fullfill or reject promise }); so above promise receives callback function called executor function signature function(fullfill, reject).
a newly created promise has important property function called then used chaining , controlling logic flows.
then takes 2 optional callback parameters onfulfilled , onrejected.
inside executor function 2 things happens indicate outcome of promise -
fullfillmethod gets called or without value:means operation completed successfully. if call fulfill value
onfulfilledcallback inthenreceive value, if decided not provide value infulfillcallonfulfilledcalled parameterundefined.var promise = new promise(function(fulfill, reject) { // lets assume operation completed fulfill('success'); }); promise.then(onfulfilled, onrejected); function onfulfilled(result) { console.log(result); // success printed }rejectmethod gets called or without value:
problem occurred while performing operation. can decided whether pass error messagerejectcallback indicate error occurred end user.var promise = new promise(function(fulfill, reject) { // lets assume operation did not complete reject(new error('error')); }); promise.then(onfulfilled, onrejected); function onrejected(error) { console.log(error.message); // error message printed }
now lets talk promise.resolve.
at top learned how create promise through constructor.
var promise = new promise(function (fulfill, reject) { fulfill('success value'); }); // now: promise.resolve // same thing above code var promise = promise.resolve('success value'); similarly comes promise.reject -
var promise = new promise(function (fulfill, reject) { reject(new error('error value')); }); var promise = promise.reject(new error('error value')); in case save seems returning promise , internally promise may calling either fulfill or reject method don't need call promise.resolve(). need values returned promise either fulfilled value or rejected value in then method.
savedata : function() { this.$.document.data = this.$.message.value; // return promise return this.$.document.save("/parent", "child"); } savedata() .then(function() { console.log('sent event!!!!!!'); this.$.document.reset(); }.bind(this)); i hope makes things promises clearer.
Comments
Post a Comment