[英]Javascript ES6 promise inside class function
After having read pages and pages about promises, I still cannot figure out the right way to return a promise (ES6 spec) inside a class function. 在阅读了有关promise的页面和页面之后,我仍然无法找出在类函数内返回promise(ES6规范)的正确方法。
Here are the various options I've tried. 这是我尝试过的各种选项。
utils.getMyPhotos()
returns a promise. utils.getMyPhotos()
返回一个承诺。
1) Return the promise AND it's values
1)兑现诺言及其价值
profilePictures(){
return utils.getMyPhotos().then(function(result){
var photosArray = result;
photosArray.forEach(function(element) {
this._list.push(new Picture(
element.src,
element.caption
));
}, this);
return this._list;
},function(error){
console.log(error);
return error;
});
}
2) Only return the promise' values
2)只返回诺言的值
profilePictures(){
utils.getMyPhotos().then(function(result){
var photosArray = result;
photosArray.forEach(function(element) {
this._list.push(new Picture(
element.src,
element.caption
));
}, this);
return this._list;
},function(error){
console.log(error);
return error;
});
}
3)Create a new Promise and return it
3)创建一个新的Promise并返回
profilePictures(){
return new Promise(function(fulfill,reject){
utils.getMyPhotos().then(function(result){
var photosArray = result;
photosArray.forEach(function(element) {
this._list.push(new Picture(
element.src,
element.caption
));
}, this);
fulfill(this._list);
},function(error){
console.log(error);
reject(error);
});
}
}
I try to use the above function as following : 我尝试使用上述功能,如下所示:
pictures.profilePictures().then(function(result){
console.log("all good");
console.dump(result);
},function(error){
console.log("errors encountered");
console.log(error);
});
Hoever in the CLI I only see "errors encountered" followed by an empty error
object. 但是在CLI中,我只能看到“遇到的错误”,后跟一个空的
error
对象。
What am I doing wrong? 我究竟做错了什么?
this._list
and return the result of doing this, as it is not clear from the function name that this is what it will do. this._list
突变, 并返回执行此操作的结果,因为从函数名称中尚不清楚这是它将执行的操作。 Or maybe I'm being nit-picky. ______ ______
Refactor of (1) 重构(1)
I have moved the error handler so that it will catch errors in all previous handlers. 我已经移动了错误处理程序,以便它将捕获以前所有处理程序中的错误。 And we throw the error after logging it so we can catch errors on consumption of the API, rather than handling them internally.
而且我们在记录错误后将其抛出,因此我们可以捕获使用API时发生的错误,而不是在内部进行处理。 Though this may not be what you want to do, it means errors don't get swallowed mysteriously.
尽管这可能不是您想要执行的操作,但这意味着错误不会被神秘吞没。
profilePictures () {
return utils.getMyPhotos()
.then(function (result) {
return result.map(function (element) {
return new Picture(element.src, element.caption);
});
})
.then(null, function (error){
console.log(error);
throw err;
});
}
Consume it: 消耗它:
instance.profilePictures()
.then(function (pics) {
pics.forEach(function (pic) {
// Do something with the Picture instance
});
})
.then(null, function (err) {
// Handle the error
});
1) Return the promise AND it's values
1)兑现诺言及其价值
Yes! 是! You always need to
return
promises from asynchronous functions, and from then
callbacks you need to return values (or promises for them) to make them available to the next function in the chain. 你总是需要
return
从异步功能的承诺,并从then
回调你需要返回值(或承诺他们的),以使其可在链中的一个功能。
2) Only return the promise' values
2)只返回诺言的值
That doesn't work . 那没用 。
3) Create a new Promise and return it
3)创建一个新的Promise并返回
That can work but is an antipattern . 那可以工作,但是是反模式 。 Avoid it.
躲开它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.