码迷,mamicode.com
首页 > 其他好文 > 详细

promise解决异步嵌套问题

时间:2019-07-04 11:30:12      阅读:89      评论:0      收藏:0      [点我收藏+]

标签:col   type   post   syn   string   promise   The   解决   span   

   function a(){
      return new Promise(function(res,rej){
        $.ajax({
          url:"a",
          type: "GET",
          async:true,
          dataType:"json",
          success:function(data){
            console.log(data,"a");
            res(data);
          }
        })
      });
    }
    function b(data){
      console.log(data,"data");
      return new Promise(function(res,rej){
        $.ajax({
            url:"b",
            type: "POST",
            async:true,
            data:JSON.stringify(data),
            dataType:"json",
            success:function(data){
              console.log(data,"b");
              res();
            }
          })
      });
    }
    $("#btn").click(function(){
      a().then(function (data){
        b(data);
      }).then(function(){
      })
    })

b的请求结果依赖于a的数据

promise解决异步嵌套问题

标签:col   type   post   syn   string   promise   The   解决   span   

原文地址:https://www.cnblogs.com/celine-huang/p/11130942.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!