How to avoid nesting structure of callbacks with promises? [finished]

半城伤御伤魂 提交于 2021-02-05 08:59:05

问题


I am using promises to avoid the nesting structure created by callbacks.

However in this code I still have some nesting. Is there something I am doing wrong or is this un-avoidable in this case?

In this case I want to check and see if a profile exists and if it does not I want to create it.

  DB.getProfile(id_google).then((resGet) => {
    if(!resGet[0]){
      console.log('PROFILE - NOT FOUND - MUST CREATE');

      DB.createProfile(id_google, email, name, pic_url).then((resCreate)=>{
        console.log('PROFILE CREATED');
      }).catch((error) => {
        console.log('ERROR - createProfile() Failed: ', error);
      });

    } else {
      console.log('PROFILE FOUND LOCALLY');
      console.log(resGet[0]);
      return done(null, resGet[0])
    }
  }).catch((error) => {
      console.log('ERROR - getOrCreateProfile() Failed: ', error);
  });
};

回答1:


You can return and chain using multiple then

DB.getProfile(id_google)
    .then((resGet) => {
        if (!resGot[0]) {
            return DB.createProfile(id_google, email, name, pic_url);
        }
        return resGot[0];
    })
    .then((res) => {
        callback(null, res)
    })
    .catch((error) => {
        console.log('ERROR - getOrCreateProfile() Failed: ', error);
    });

If resGot[0] exist, then it is returned, and in the second then the variable res is that value. If it does not, then the promise of createProfile is returned and the value of res is whatever that function returns




回答2:


Sometimes it helps to boil your code down to the essentials:

getProfile
  if not found, 
    createProfile
       return profile
  else
    done profile

Presumably, you want to get createProfile outside in the same chain as the rest of the promise.

I changed the structure to:

getProfile
  if found, return profile 
  createProfile
    return profile
then
  done(profile)

It isn't really possible to have just one degree of nesting in this case. But you can reduce some level of nesting.

DB.getProfile(id_google)
.then((resGet) => {
    if(resGet[0]) {
      console.log('PROFILE FOUND LOCALLY');
      return resGet[0];
    }
    console.log('PROFILE - NOT FOUND - MUST CREATE');
    return DB.createProfile(id_google, email, name, pic_url)
    .then((resCreate)=>{
      console.log('PROFILE CREATED');
      return resCreate[0]; //Assuming resCreate looks like resGet
    })
  }
})
.then(profile=> {
    //The done function which belongs to passport is called once here.
    console.log(profile);
    return done(null, resGet[0])
})
.catch((error) => {
    console.log('ERROR - getOrCreateProfile() Failed: ', error);
});


来源:https://stackoverflow.com/questions/47917933/how-to-avoid-nesting-structure-of-callbacks-with-promises-finished

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!