首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >catch Promise:在ES6 ()之后调用了catch()

catch Promise:在ES6 ()之后调用了catch()
EN

Stack Overflow用户
提问于 2018-02-11 17:03:29
回答 2查看 91关注 0票数 0

下面代码中的Promise可以正常工作,Ajax请求(load() jQuery-function)也可以工作:

代码语言:javascript
复制
changeFragment(newFragment, fragmentUrn) {
    new Promise((resolve, reject) => {
        this.$MainContent.load(fragmentUrn, (response, status, xhr) => {
            //status === 'error' ? reject(xhr) : resolve();
            console.log(status == 'success');
            if (status === 'success'){
                console.log('resolve');
                resolve();
            }
            if (status === 'error') {
                console.log('reject');
                reject(xhr);
            }
        });
    }).then(() => {
        console.log('then');
        let newFragment = newFragment.createInstance();
        newFragment.initialize();
    }).catch((xhr) => {
        console.log('Why!?');
        console.error(`Error : ${xhr.status} ${xhr.statusText}`);
    });
}

然而,在then()之后,不知何故也调用了catch()。我在控制台中得到了以下输出:

代码语言:javascript
复制
true
resolve
then
Why!?
Error: undefined undefined
EN

回答 2

Stack Overflow用户

发布于 2018-02-12 18:15:03

你需要做这样的事情

Promise.resolve() .then(function() { return Function1().catch(errorHandler1);})

票数 0
EN

Stack Overflow用户

发布于 2018-02-11 18:04:21

我认为因为你没有返回promise,所以尝试返回promise,而不是仅仅用“new”来初始化它。

“‘then”需要等待将返回的承诺

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48729924

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档