Working with promises inside an if/else

后端 未结 10 904
北恋
北恋 2021-02-02 10:46

I have a conditional statement in which I need to perform one of two operations, then continue after whichever operation has resolved. So my code currently looks as follows:

10条回答
  •  无人共我
    2021-02-02 11:36

    How I want to improve on other answers:

    • keep it clean and simple
    • no unneeded variables
    • return promise asap
    • in js we use camelCase
    • put it in a function and name that function to keep it readable
    • let then execute moreCode so it's called after the thing is done.

    function doTheThing () {
      if (shouldDoA) return doThingA()
      else return doThingB()
    }
    
    doTheThing().then(moreCode)
    

提交回复
热议问题