How to wait for ajax success handler to finish before executing another

前端 未结 4 1319
梦如初夏
梦如初夏 2021-01-18 21:50

I have the following scenario in my javascript:

  1. Ajax loads a record.
  2. Based on the return value of that record, another ajax call grabs a html form tha
4条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-18 22:49

    Bit late to answer, but

    Much better way is to use promises as the way they designed to be used

    Pseudo implementation

    var promise = $.ajax({
        url: url,
        data: data
    }).done(function (data) {
        var result = process(data);
        return $.ajax({
            url: url,
            data: result
        });
    }).done(function (data) {
        // data is the result of second ajax call
    });
    

提交回复
热议问题