Run two functions with Ajax calls sequentially

前端 未结 5 1709
难免孤独
难免孤独 2021-01-26 09:52

I have two functions which has ajax calls inside them.

function a(){
    ajaxCall_A();
}
function b(){
    ajaxCall_B();
}

And I

5条回答
  •  春和景丽
    2021-01-26 10:12

    Make sure that ajaxCall_A() and ajaxCall_B() return promises, and add returns to a() and b().

    function a(){
        return ajaxCall_A();
    }
    function b(){
        return ajaxCall_B();
    }
    

    Then, a() and b() will execute sequentially as follows :

    function c() {
        a().then(b);
    }
    

    You should also add a return to c() so it returns a promise to its caller.

    function c() {
        return a().then(b);
    }
    

提交回复
热议问题