注册 登录
编程论坛 JavaScript论坛

promise.all 相关的问题请教

a897588006 发布于 2022-07-19 01:23, 729 次点击
我有如下的一段代码:
...;
list_a.forEach(function(aaa){
    $.ajax({
        ...,
        success: function(bbb){
            list_b.forEach(functioin(ccc){
                $.ajax({
                    ...,
                    success: function(data){...}
                })
            })
        }
    })
})
我应该如何在所有的data 都拿到之后 再进行其他操作?
我的想法是把第二层的ajax封装到promise中,再经过promis.all方法拿到所有的data数据,但是不知道怎么实现,请大佬们赐教!!
1 回复
#2
byte_20232023-05-17 17:58
程序代码:

function demo (url) {
    let _data = [];
    $.ajax({
        url,
        success: function (res){
            if (res.code === 200) {
                _data = res.data;
            }
            
        }
    })
   
    return _data;
}
const requestArr = [];
const list_a = ["https://localhost/api/settings/topmessage","https://localhost/api/settings/topmessage"]
list_a.forEach(function (url) {
    requestArr.push(demo(url))
})

Promise.all(requestArr).then(res => {
    console.log('res',res);
}).catch(err=> {
console.log('err', err)
});


摘抄:https://www.

[此贴子已经被作者于2023-5-17 18:00编辑过]

1