I have the following sample code, but it is empty after resolve, because several asynchronous functions are used in for, resulting in only one result. The main purpose is to learn Promise
.var arr1= [
{id:1,name:"Jason", age: 23, content:""},
{id:2,name:"Jason2", age: 23, content:""},
{id:3,name:"Jason3", age: 23, content:""},
{id:4,name:"Jason4", age: 23, content:""},
{id:5,name:"Jason5", age: 23, content:""},
{id:6,name:"Jason6", age: 23, content:""},
{id:7,name:"Jason7", age: 23, content:""},
{id:8,name:"Jason8", age: 23, content:""},
{id:9,name:"Jason9", age: 23, content:""},
{id:10,name:"Jason11", age: 23, content:""},
{id:11,name:"Jason12", age: 23, content:""},
{id:12,name:"Jason13", age: 23, content:""},
{id:13,name:"Jason14", age: 23, content:""},
{id:14,name:"Jason15", age: 23, content:""},
{id:15,name:"Jason16", age: 23, content:""},
{id:16,name:"Jason17", age: 23, content:""},
{id:17,name:"Jason18", age: 23, content:""},
{id:18,name:"Jason19", age: 23, content:""},
{id:19,name:"Jason22", age: 23, content:""},
{id:20,name:"Jason23", age: 23, content:""},
{id:21,name:"Jason24", age: 23, content:""},
{id:22,name:"Jason25", age: 23, content:""},
{id:23,name:"Jason26", age: 23, content:""},
{id:24,name:"Jason27", age: 23, content:""},
{id:25,name:"Jason28", age: 23, content:""},
{id:26,name:"Jason29", age: 23, content:""},
{id:27,name:"Jason33", age: 23, content:""},
{id:28,name:"Jason34", age: 23, content:""},
{id:29,name:"Jason35", age: 23, content:""},
{id:30,name:"Jason36", age: 23, content:""},
{id:31,name:"Jason37", age: 23, content:""},
{id:32,name:"Jason38", age: 23, content:""},
{id:34,name:"Jason39", age: 23, content:""},
{id:35,name:"Jason44", age: 23, content:""},
{id:36,name:"Jason41", age: 23, content:""},
{id:37,name:"Jason42", age: 23, content:""},
{id:38,name:"Jason43", age: 23, content:""}
]
var arr2 = [
{class:"2", userId: 33},
{class:"3", userId: 3},
{class:"2", userId: 5},
{class:"6", userId: 38},
{class:"2", userId: 21},
{class:"3", userId: 2},
{class:"2", userId: 1},
{class:"9", userId: 5},
{class:"2", userId: 7},
{class:"1", userId: 10},
{class:"2", userId: 13},
{class:"5", userId: 23}
]
var arr3 = [
{scope:80, userId: 21},
{scope:50, userId: 5},
{scope:70, userId: 10},
{scope:20, userId: 23},
{scope:70, userId: 13},
{scope:44, userId: 3},
{scope:23, userId: 33},
{scope:76, userId: 1},
{scope:45, userId: 10},
{scope:26, userId: 38},
{scope:65, userId: 7},
{scope:45, userId: 30},
{scope:76, userId: 22},
{scope:46, userId: 4},
{scope:95, userId: 8},
{scope:100, userId: 9},
{scope:74, userId: 35}
]
//ID
function getName() {
return new Promise((resolve, reject) => {
let cf = []
arr1.map(item => {
cf.push({id: item.id,name:item.name})
})
resolve(cf)
})
}
//
function getScope(id) {
return new Promise((resolve,reject) => {
arr3.map(item=>{
if(id == item.userId){
resolve(item.scope)
}
})
})
}
function allPromise() {
return new Promise((resolve, reject) => {
let data = []
getName().then(res => {
console.log("res", res)
let promise1 = []
for (var i = 0; i < res.length; iPP) {
let name = res[i].name
let id = res[i].id
let classes, scope
for (var j = 0; j < arr2.length; jPP) {
if (arr2[j].userId == id) {
classes = arr2[j].class
getScope(id).then(result => {
scope = result
data.push({
id: id,
name: name,
class: classes,
scope: scope
})
})
}
}
}
})
console.log(data)
resolve(data)
})
}