Load pictures according to the order of the picture array (load one and then load the next)

There is an array:
const imgs = ['url1', 'url2', 'url3', ...];
Please achieve the effect:
Please load the pictures in the order of the picture array (Note: load one and then load the next)

const images = ['url1','url2','url3']

const loadImg = (url) => {
 return new Promise(resolve => {
   let image = new Image()
   image.src = url
   image.onload = () =>{
     resolve(image)
   }
   image.onerror = () => {
     reject(`Errors on loading`)
   }
 }) 
}

const solve = (images) => {
    const imgQueue = []
    for(let i = 0;i<images.length;i++) {
      imgQueue.push(loadImg(images[i]))
    }
    Promise.all(imgQueue).then((item) => {
      console.log(item)
    })
}

solve(images)

Published 91 original articles · Like 82 · Visits 10,000+

Guess you like

Origin blog.csdn.net/qq_42893625/article/details/104974379