conditional promise before then()

Viewed 49

i need to use one promise to retrieve some data before continue with the logical of my code.

The thing ins i try to use this:

if (label === 'edit') {
    new skill_watson_export_no_append(jsonSkillIntentsUpdate, id_skill_tst).update_skill_no_append(jsonSkillIntentsUpdate, id_skill_tst)
} else {
    new skill_watson_export(new_intent_preprocess, id_skill_tst).update_skill_append(new_intent_preprocess, id_skill_tst)
}.then(response_watson => {

but i have an error 'Declaration or statment expected'

i need to use one of this promises before continue, if not the only way is to repeat 200 lines of code. Someone help me....? thanks

1 Answers

if statements don't provide a value when they are evaluated. You can't follow on with a method call.

You could use a conditional operator:

(
    (label === 'edit') ?
        new skill_watson_export_no_append(jsonSkillIntentsUpdate, id_skill_tst).update_skill_no_append(jsonSkillIntentsUpdate, id_skill_tst) :
        new skill_watson_export(new_intent_preprocess, id_skill_tst).update_skill_append(new_intent_preprocess, id_skill_tst)
).then( ... )

But, frankly, that is rather unwieldy, and assigning the promise to a variable declared with let would probably be easier to manage.

Breaking the logic out into its own function that returns the apropriate promise would likely be even easier to maintain.

Related