Angular / TypeScript - Call a function after another one has been completed

后端 未结 3 1161
别那么骄傲
别那么骄傲 2020-12-24 08:35

I would like to call f2 after f1 has been completed. f1 function can be synchronous or asynchronous.

3条回答
  •  臣服心动
    2020-12-24 08:54

    If f1 is synchronous, there is nothing special to do:

    global() {
        f1();
        f2();
    }
    

    If f1 is asynchronous and return an Observable, use Rxjs operator, like concatMap:

    global() {
        f1().concatMap(() => f2());
    }
    

    If f1 is asynchronous and return a Promise, use async/await:

    async global() {
        await f1();
        f2();
    }
    

    If f1 is asynchronous and return a Promise (alternative):

    global() {
        f1().then(res => f2());
    }
    

提交回复
热议问题