Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript ES6承诺类内函数_Javascript_Promise_Es6 Promise - Fatal编程技术网

Javascript ES6承诺类内函数

Javascript ES6承诺类内函数,javascript,promise,es6-promise,Javascript,Promise,Es6 Promise,在阅读了很多关于承诺的文章之后,我仍然无法找到在类函数中返回承诺(ES6规范)的正确方法 以下是我尝试过的各种选择。 utils.getMyPhotos()返回一个承诺 1) 回报承诺及其价值观 2) 只返回承诺的值 3) 创建一个新的承诺并回报它 我尝试按如下方式使用上述功能: pictures.profilePictures().then(function(result){ console.log("all good"); console.dump(resul

在阅读了很多关于承诺的文章之后,我仍然无法找到在类函数中返回承诺(ES6规范)的正确方法

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

1) 回报承诺及其价值观

2) 只返回承诺的值

3) 创建一个新的承诺并回报它

我尝试按如下方式使用上述功能:

pictures.profilePictures().then(function(result){
        console.log("all good");
        console.dump(result);
    },function(error){
        console.log("errors encountered");
        console.log(error);
    });
在CLI中,我只看到“遇到错误”,后面跟着一个空的
错误
对象

我做错了什么

  • 您在这里做的是正确的,但是您的错误处理程序不会从其同级成功处理程序捕获错误。我还建议您不要对实例变量
    this.\u list
    进行变异,并返回执行该操作的结果,因为函数名中不清楚这是它将执行的操作。或许我是在吹毛求疵。无论如何,请参见下面的建议重构
  • 你什么也不退。(有关返回的重要性,请参见!)
  • 是一种反模式的承诺 ______

    重构(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) 回报承诺及其价值观

    是的始终需要
    从异步函数中返回
    承诺,然后从
    回调中返回值(或承诺),使其可用于链中的下一个函数

    2) 只返回承诺的值

    3) 创建一个新的承诺并回报它


    这可以奏效,但是。避免它。

    他使用的是本地es6承诺,而不是bluebird@mmm我不认为我在重构中使用了任何Bluebird?编辑:啊,关于反模式的链接?我不认为这不是蓝鸟特有的。有没有什么特别的原因让你不使用
    catch
    而不是
    then(null,…)
    ?@LUH3417某种程度上证明
    then
    错误处理程序不会从它的兄弟成功处理程序中捕获错误。@aaronuall:这与
    catch(function(error){…}
    如果有帮助的话。另请参见他所指的内容。“仅返回承诺值”一个
    promise
    仅包含一个值,您无法将其展开(除非传递给
    then
    的函数使用了副作用),因为
    then
    会自动返回一个
    promise
    pictures.profilePictures().then(function(result){
            console.log("all good");
            console.dump(result);
        },function(error){
            console.log("errors encountered");
            console.log(error);
        });
    
    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
        });