代码之家  ›  专栏  ›  技术社区  ›  user4035

如何等到所有的承诺完成

  •  0
  • user4035  · 技术社区  · 5 年前

    假设,我们想在上传图像之前检查图像的宽度。 Here is the fiddle

    我试过了,但也没用。 Fiddle .

    $(document).ready(function () {
        function check_image_dimensions ($this, image, img_name) {
    
            let min_width = + $this.data('min_width');
            if (typeof(min_width) === "number" && image.width < min_width) {
                alert(`Error loading photo ${img_name}, image width: ${image.width}px is less then minimal: ${min_width}px.`);
                return false;
            }
    
            return true;
        }
    
        function unknown_error () {
            alert('Unknown error while loading image.');
        }
    
        $(document).on('change', '.upload_images', function() {
            var $this = $(this);
            let images = $this.prop('files');
            if (typeof(images) === 'undefined' || images.length === 0) {
                return false;
            }
    
            let good_images = Array();
            let promise = Promise.resolve();// <- I tried this, but it is wrong
            for (let i = 0; i < images.length; i++) {
                let promise = new Promise(function(resolve, reject) {
                    const reader = new FileReader();
                    reader.onload = resolve;
                    reader.readAsDataURL(images[i]);
                });
    
                promise.then((event) => {
                    const img  = new Image();
                    img.onload = () => {
                        if(check_image_dimensions($this, img, images[i].name) === true) {
                            good_images.push(i);
                        }
                    };
    
                    img.onerror = unknown_error;
                    img.src = event.target.result;
                    return i;
                }).catch(unknown_error);
            }
    
            promise.then(() => alert(good_images));
        });
    });
    
    0 回复  |  直到 5 年前
        1
  •  1
  •   Wex    5 年前

    如果要并行处理所有这些图像,可以创建一个承诺数组并使用 Promise.all . 请注意,在处理图像时,还需要将该部分包装成一个承诺:

    const good_images = [];
    const promises = [];
    for (let i = 0; i < images.length; I++) {
      const image = images[I];
      let promise = new Promise( ... );
    
      promise = promise.then((event) => {
        const img = new Image();
        img.src = event.target.result;
    
        return new Promise(resolve => {
          img.onload = () => {
            if (is_good(img)) {
              good_images.push(i);
            }
            resolve();
          };
        });
      })
    
      promise = promise.catch(() => { ... });
    
      promises.push(promise);
    });
    
    Promise.all(promises).then(() => alert(good_images));
    
        2
  •  0
  •   Mauricio Sipmann    5 年前

    let arrdata = [1,2,3,4];
    let prom = Promise.resolve();
    
    arrdata.map(data => {
    
      // Here's the deal
      prom = prom.then(() => {
        console.log(data);
      });
    });
    
    prom.then(() => console.log('end'));