Working with promises inside an if/else

后端 未结 10 888
北恋
北恋 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:38

    You can use async/await

    async function fn() {
      let p, result;
      if (shoud_do_thing_a) {
        p = await do_thing_a()
      } else {
        p = await do_thing_b()
      }
      if (p) {
        result = more_code();
      }
      return result
    }
    

提交回复
热议问题