-2

My code looks like this,

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

    }; }

the getAdminUgData is,

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

    });
});}

I am supposed to print out 'resolve' only because the resolve(r); is confirmed being called in getAdminUgData. But the confusing/weird reality is that 'resolve!!' is printed and after that, '!!reject' is also printed. And I completely have no ideas about this. Any suggestions are welcome; thanks.

Yu Fang
  • 520
  • 5
  • 17
  • Any chance that the callback that has the `getData()` call is invoked twice? Put another log in there to make sure. – Bergi Mar 28 '18 at 03:08
  • @Bergi I've checked and getData is invoked once. – Yu Fang Mar 28 '18 at 03:33
  • 1
    what happens if you, instead of `getData().then.....` use `getAdminUgData().then.....` ... I mean, the whole `async function getData(){ return await getAdminUgData();}` is pointless anyway - of course, we won't mention the missing `}` in your question which would mean the code didn't run at all .... – Jaromanda X Mar 28 '18 at 03:52
  • @JaromandaX thanks, but, even if i remove getData and add async and await to getAdminUgData, nothing changes. – Yu Fang Mar 28 '18 at 06:06
  • I didn't say add async/await anywhere - there's no need for async/await at all in that code - it's just complicating a very simple promise chain – Jaromanda X Mar 28 '18 at 07:02

1 Answers1

2

No, it's absolutely impossible for the same promise to both reject and fulfill - and so it will never happen that both callbacks to .then(…, …) are called. It is however totally possible that both a .then(…) and a .catch(…) callback are called when chained - notice that this doesn't seem to be the case in your example, it seems there's something else going on.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • I printed the error in the catch, and find that the root cause has nothing to do with the promise. the promise works properly, and it is somewhere else triggering the catch. Thanks a lot for useful post info. – Yu Fang Mar 28 '18 at 07:01