【问题标题】:Is it possible that promise' reject and resolve callbacks are called both?是否有可能同时调用承诺的拒绝和解决回调?
【发布时间】:2018-03-28 11:13:46
【问题描述】:

我的代码是这样的,

export function handleLogin(window,userData){
    return (dispatch) => {
        Meteor.call('checkUserLogin',userData,
            (error,result)=>{
                if(result.isLogin && !error) {             
                    Meteor.call('SOMECALL',SOMEDATA (e,r)=>{
                    if(!e) {                           
                            async function getData(){ return await getAdminUgData();}
                            getData()
                            .then((d)=>{console.log('resolve!!');})
                            .catch((e)=>{console.log('!!reject'); });
                        }
        });
        });

    }; }

getAdminUgData 是,

export function getAdminUgData(){
return new Promise((resolve, reject) => {
    Meteor.call('adminGetUserGroupData', (e,r)=>{
        if(e) reject(new Error('error'));
        else resolve(r);        

    });
});}

我应该打印出'resolve'只是因为resolve(r);确认在 getAdminUgData 中被调用。但令人困惑/奇怪的现实是“解决!!”被打印,然后,'!!reject' 也被打印。我对此完全没有任何想法。欢迎提出任何建议;谢谢。

【问题讨论】:

  • getData() 调用的回调是否有可能被调用两次?在那里输入另一个日志以确保。
  • @Bergi 我已经检查过了,getData 被调用了一次。
  • 如果你使用 getAdminUgData().then..... 而不是 getData().then..... 会发生什么...我的意思是,整个 async function getData(){ return await getAdminUgData();} 无论如何都是毫无意义的 - 当然,我们不会提及缺少的 }在你的问题中,这意味着代码根本没有运行......
  • @JaromandaX 谢谢,但是,即使我删除 getData 并添加异步并等待 getAdminUgData,也没有任何变化。
  • 我没有说在任何地方添加 async/await - 在代码中根本不需要 async/await - 它只是让一个非常简单的 Promise 链复杂化

标签: javascript meteor async-await es6-promise


【解决方案1】:

不,it's absolutely impossible for the same promise to both reject and fulfill - 因此不会发生对.then(…, …) 的两个回调都被调用的情况。然而,totally possible that both a .then(…) and a .catch(…) callback are called when chained - 请注意,在您的示例中似乎并非如此,似乎还有其他事情发生。

【讨论】:

  • 我在catch中打印了错误,发现根本原因与promise无关。承诺正常工作,它在其他地方触发了捕获。非常感谢您提供有用的帖子信息。
猜你喜欢
  • 1970-01-01
  • 2015-01-18
  • 2015-09-27
  • 2013-06-22
  • 2014-05-27
  • 2023-03-26
  • 2019-08-06
  • 2016-10-20
  • 2021-08-17
相关资源
最近更新 更多