Javascript class 函数中的 ES6 promise

Javascript ES6 promise inside class function

在阅读了关于 promise 的一页又一页之后,我仍然无法找到在 class 函数中 return promise(ES6 规范)的正确方法。

以下是我尝试过的各种选项。 utils.getMyPhotos()return一个承诺。

1) Return the promise AND it's values

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

 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

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

我尝试使用上面的函数如下:

pictures.profilePictures().then(function(result){
        console.log("all good");
        console.dump(result);
    },function(error){
        console.log("errors encountered");
        console.log(error);
    });

但是在 CLI 中我只看到 "errors encountered" 后跟一个空的 error 对象。

我做错了什么?

  1. 您在这里做的是正确的,但是您的错误处理程序不会从其兄弟成功处理程序中捕获错误。我还建议您不要改变实例变量 this._list and return 这样做的结果,因为从函数名称中不清楚这就是它要做的。或者也许我很挑剔。无论如何,请参阅下面的推荐重构。
  2. 你什么都没有return。 (请参阅 了解 return 的重要性!)
  3. 是一个 Promise 反模式。 Watch out for those.

______

重构 (1)

我已经移动了错误处理程序,以便它可以捕获所有以前的处理程序中的错误。我们在记录错误后抛出错误,这样我们就可以在使用 API 时捕获错误,而不是在内部处理它们。虽然这可能不是您想做的,但这意味着错误不会被神秘地吞没。

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

食用它:

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

是的! 你总是需要 return 来自异步函数的 promises,而来自 then 回调你需要 return 值(或为他们承诺)使他们可用于链中的下一个函数。

2) Only return the promise' values

That doesn't work.

3) Create a new Promise and return it

可以,但是 is an antipattern。避开它。