列举三类
第一类:很多个异步,依次执行,要有顺序
- 方法一 使用加载事件特性(img的src变化,load事件执行)
function getImage(num){
let img = new Image()
img.src = `./image/${num}.png`
img.num = num
img.arr = []
img.addEventListener('load',loadHandle)
}
function loadHandle(e){
let cloneImg = e.currentTarget.cloneNode()
this.arr.push(cloneImg)
this.num++
if(num > 59) return this.arr
that.src = `./image/${this.num}.png`
}
function getImage2(num){
return new Promise((resolve,reject)=>{
let img = new Image()
img.src = `./image/${num}.png`
img.onload = function(){
resolve(img)
}
})
}
function main(total){
let arr = []
for(let i = 0; i < total; i++){
let img = getImage2(i)
arr.push(img)
}
if(arr.length === 56){
Promise.all(arr).then(list=>{
console.log(list)
})
}
}
function getImage1(num){
return new Promise((resolve,reject)=>{
let img = new Image()
img.src = `./image/${num}.png`
img.onload = function(){
resolve(img)
}
})
}
async function main(total){
let arr = []
for(let i = 0; i < total; i++){
let img = await getImage(i)
arr.push(img)
}
}
function loadFile(item){
return new Promise((resolve,reject)=>{
let { name, url, fileId, size } = item
let temp = new Audio(url)
temp.onloadedmetadata = function(){
resolve({
audioName: name.slice(0,40),
audioUrl: url,
fileId: fileId,
fileSize: size,
duration: temp.duration
})
}
temp.onerror = function(){
resolve({
audioName: name.slice(0,40),
audioUrl: url,
fileId: fileId,
fileSize: size,
duration: temp.duration || 0
})
}
})
}
const onChange =async (res) => {
let params = [], fileList = Array.prototype.slice.call(res)
for(let i = 0; i < fileList.length; i++){
let result = await loadFile(fileList[i])
params.push(result)
}
addAudio({list: params}).then(res=>{
if(res && res.code === 100){
message.success('上传成功')
updataList()
}
})
}
第二类:很多个异步,只要最后所有的执行结果
PromiseSelf.all = function(list){
return new PromiseSelf((resolve,reject)=>{
let done = gen(resolve,list.length)
list.forEach((item,index)=>{
item.then(value=>{
done(index,value)
})
})
})
}
function gen(resolve,length){
let values = []
return function(i,value){
values[i] = value
if(i === length - 1){
resolve(values)
}
}
}
第三类:很多个异步,只要最后一次的执行结果(适用场景: 搜索时,每次输入要调用接口,接口得同步)
constructor(props){
super(props)
this.state = {
searchResult: ''
}
this.queueList = []
}
handleSearch(type){
return async (value)=>{
this.pushQueue(value, []);
let result = await this.handleSearchAPI(type, value);
this.queueList.forEach(element => {
if(element.key == value){ element.result = result; }
});
let searchResult = this.searchQueue[this.searchQueue.length - 1].result;
this.setState({ ...searchResult, });
}
}
pushQueue(key, result){
this.queueList.push({key, result});
}
handleSearchAPI(type, value, scroll = 0){
return new Promise((resolve, reject) =>{
trainCampSchool({
word:value || '',
pageNum: this.searchPageNum,
pageSize: this.searchPageSize,
}).then(res=>{
let schoolDataList=this.state.school.concat(res.data.dataList)
resolve({
school: schoolDataList
scrollFlag:true
})
}).catch(err =>{
reject(err);
})
} )
}
<Select
showSearch
style={{ width: 136 }}
placeholder="学校名称"
allowClear={true}
filterOption={false}
value = {this.state.schoolName }
onChange={this.handleChange('school')}
onFocus={this.handleFocus('school')}
onSearch={this.handleSearch('school')}
onPopupScroll={this.handlePopupScroll('school')}
getPopupContainer={triggerNode => triggerNode.parentElement}
>
{
this.state.school?.map((item)=>
<Option value={item.schoolId} key={item.schoolId}>{item.fullName}</Option>
)
}
</Select>
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!