Home > Enterprise >  Modifying scripts of branching
Modifying scripts of branching

Time:10-14

I want to do away with branching operators, but I don't know how I could optimize my script, I'm wondering, Promise to use? But inside it I'll write branching operators again, I'm very interested to hear your point of view on how I could do it, I couldn't find examples of my question, but I'm very interested in how I could optimize it more. I'm a newbie, don't judge me too harshly.

Again, I tried to create a Promise but I know that again I will put branching operators in it.

I would be grateful for any advice.

const User = await User.findByPk(ctx.match[1]);

await User.update({
  Value_U: ctx.match[2] == 'accepted' ? 1 : 2,
});

if (User.Value_U == 1) {
  ctx.reply('Success')
}

if (User.Value_U == 2) {
  ctx.reply('Reject')
}

if (User.Value_U == 3) {
  ctx.reply('OK')
}

const p1 = new Promise((resolve, reject) => {

})

CodePudding user response:

You can use an array or object containing functions, and index them using User.Value_U.

let actions = {
    1: () => ctx.reply('Success'),
    2: () => ctx.reply('Reject'),
    3: () => ctx.reply('OK')
};

action[User.Value_U]();
  • Related