需要帮助理解这个承诺和处理错误
Need help understanding this promise and handling the error
我正在将一些数据保存到我的 Polymer 元素内的 Firebase 数据库中。一切正常。但是作为 Promises 的新手,我需要帮助来理解 Promise.resolved()
在方法结束时的含义。使用 .then
时,承诺不是在此之前完成吗?那么这到底在做什么呢?我查看了 around,但找不到 resolved()
没有值的示例。
我怎样才能将其更改为更熟悉的结构,如下所示:
.then(function(snapshot) {
// The Promise was "fulfilled" (it succeeded).
}, function(error) {
// The Promise was rejected.
});
这是从 documentation 中提取的块和 Promise.resolved()
:
saveData : function() {
this.$.document.data = this.$.message.value;
this.$.document.save("/parent", "child").then(function() {
console.log('sent the event!!!!!!');
this.$.document.reset();
}.bind(this));
return Promise.resolve();
},
如果你想要做到 obj.saveData().then(...)
,那么你可以 return 像这样的内在承诺:
saveData : function() {
this.$.document.data = this.$.message.value;
// return this promise
return this.$.document.save("/parent", "child").then(function() {
console.log('sent the event!!!!!!');
this.$.document.reset();
}.bind(this));
}
首先你需要了解 Promises 的基础知识。
让我们从最基础的开始 -
新创建的 es6 promise 处于以下状态之一:
- 已解决
- 拒绝
- 待定 --> 等待解决或拒绝
让我们创建一个示例 Promise
var promise = new Promise(function(fulfill, reject) {
// Do some stuff and either fullfill or reject the promise
});
所以上面的 promise 接收到一个回调函数,也称为 executor 函数,签名为 function(fullfill, reject)
。
新创建的承诺还有一个非常重要的 属性 函数,称为 then
,用于链接和控制逻辑流。
then
有两个可选的回调参数 onFulfilled
和 onRejected
.
在这个 executor 函数中,有两件事恰好表明了 promise 的结果 -
fullfill
方法被调用时有或没有值:
表示操作成功完成。如果您使用 value 调用 fulfill,那么 then
中的 onFulfilled
回调将收到该值,前提是您决定不这样做在 fulfill
调用中提供一个值,然后将使用参数 undefined
.
调用 onFulfilled
var promise = new Promise(function(fulfill, reject) {
// lets assume operation completed successfully
fulfill('Success');
});
promise.then(onFulfilled, onRejected);
function onFulfilled(result) {
console.log(result);
// Success will be printed
}
reject
方法被调用时有或没有值:
执行操作时出现了一些问题。您可以决定是否传递一些错误消息 reject
回调以指示最终用户发生错误。
var promise = new Promise(function(fulfill, reject) {
// lets assume operation did not complete successfully
reject(new Error('Error'));
});
promise.then(onFulfilled, onRejected);
function onRejected(error) {
console.log(error.message);
// Error message will be printed
}
现在让我们谈谈 Promise.resolve
。
在顶部,您学习了如何通过构造函数创建 promise。
var promise = new Promise(function (fulfill, reject) {
fulfill('Success value');
});
// Now: Promise.resolve
// Exactly does the same thing as above code
var promise = Promise.resolve('Success value');
同样是Promise.reject
-
var promise = new Promise(function (fulfill, reject) {
reject(new Error('Error VALUE'));
});
var promise = Promise.reject(new Error('Error VALUE'));
在你的情况下,save
似乎已经在内部返回一个承诺,该承诺可能正在调用 fulfill
或 reject
方法,因此你不需要调用 Promise.resolve()
。您只需要在 then
方法中获取该承诺返回的值 fulfilled
值或 rejected
值。
saveData : function() {
this.$.document.data = this.$.message.value;
// return this promise
return this.$.document.save("/parent", "child");
}
saveData()
.then(function() {
console.log('sent the event!!!!!!');
this.$.document.reset();
}.bind(this));
我希望它能让关于承诺的事情更清楚一些。
我正在将一些数据保存到我的 Polymer 元素内的 Firebase 数据库中。一切正常。但是作为 Promises 的新手,我需要帮助来理解 Promise.resolved()
在方法结束时的含义。使用 .then
时,承诺不是在此之前完成吗?那么这到底在做什么呢?我查看了 around,但找不到 resolved()
没有值的示例。
我怎样才能将其更改为更熟悉的结构,如下所示:
.then(function(snapshot) {
// The Promise was "fulfilled" (it succeeded).
}, function(error) {
// The Promise was rejected.
});
这是从 documentation 中提取的块和 Promise.resolved()
:
saveData : function() {
this.$.document.data = this.$.message.value;
this.$.document.save("/parent", "child").then(function() {
console.log('sent the event!!!!!!');
this.$.document.reset();
}.bind(this));
return Promise.resolve();
},
如果你想要做到 obj.saveData().then(...)
,那么你可以 return 像这样的内在承诺:
saveData : function() {
this.$.document.data = this.$.message.value;
// return this promise
return this.$.document.save("/parent", "child").then(function() {
console.log('sent the event!!!!!!');
this.$.document.reset();
}.bind(this));
}
首先你需要了解 Promises 的基础知识。
让我们从最基础的开始 -
新创建的 es6 promise 处于以下状态之一:
- 已解决
- 拒绝
- 待定 --> 等待解决或拒绝
让我们创建一个示例 Promise
var promise = new Promise(function(fulfill, reject) {
// Do some stuff and either fullfill or reject the promise
});
所以上面的 promise 接收到一个回调函数,也称为 executor 函数,签名为 function(fullfill, reject)
。
新创建的承诺还有一个非常重要的 属性 函数,称为 then
,用于链接和控制逻辑流。
then
有两个可选的回调参数 onFulfilled
和 onRejected
.
在这个 executor 函数中,有两件事恰好表明了 promise 的结果 -
fullfill
方法被调用时有或没有值:表示操作成功完成。如果您使用 value 调用 fulfill,那么
调用then
中的onFulfilled
回调将收到该值,前提是您决定不这样做在fulfill
调用中提供一个值,然后将使用参数undefined
.onFulfilled
var promise = new Promise(function(fulfill, reject) { // lets assume operation completed successfully fulfill('Success'); }); promise.then(onFulfilled, onRejected); function onFulfilled(result) { console.log(result); // Success will be printed }
reject
方法被调用时有或没有值:
执行操作时出现了一些问题。您可以决定是否传递一些错误消息reject
回调以指示最终用户发生错误。var promise = new Promise(function(fulfill, reject) { // lets assume operation did not complete successfully reject(new Error('Error')); }); promise.then(onFulfilled, onRejected); function onRejected(error) { console.log(error.message); // Error message will be printed }
现在让我们谈谈 Promise.resolve
。
在顶部,您学习了如何通过构造函数创建 promise。
var promise = new Promise(function (fulfill, reject) {
fulfill('Success value');
});
// Now: Promise.resolve
// Exactly does the same thing as above code
var promise = Promise.resolve('Success value');
同样是Promise.reject
-
var promise = new Promise(function (fulfill, reject) {
reject(new Error('Error VALUE'));
});
var promise = Promise.reject(new Error('Error VALUE'));
在你的情况下,save
似乎已经在内部返回一个承诺,该承诺可能正在调用 fulfill
或 reject
方法,因此你不需要调用 Promise.resolve()
。您只需要在 then
方法中获取该承诺返回的值 fulfilled
值或 rejected
值。
saveData : function() {
this.$.document.data = this.$.message.value;
// return this promise
return this.$.document.save("/parent", "child");
}
saveData()
.then(function() {
console.log('sent the event!!!!!!');
this.$.document.reset();
}.bind(this));
我希望它能让关于承诺的事情更清楚一些。