Working with promises inside an if/else

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

    more_code = miFunc() => return new Promise((resolve, reject) => ... });
    

    Solution 1

    const waitFor = should_do_thing_a ? do_thing_a() : do_thing_b();
    waitFor.then(...).catch(...)
    

    Solution 2

    let waitFor = Promise.resolve();
    
    if (do_thing_a) {
        waitFor = do_thing_a();
    } else {
        waitFor = do_thing_b();
    }
    
    waitFor.then(...).catch(...);
    

提交回复
热议问题