How to make GET requests to multiple links at the same time and obtain the values ​of the returned results in sequence for conditional judgment?

Make requests for multiple links at the same time, and conditionally judge the value of the first returned result.

If the conditions are met, the values ​​of subsequent request results will not be obtained.

If they are not met, the values ​​of the second and third returned results will be obtained in sequence.

Perform conditional judgment. If the conditions are met, the subsequent values ​​will not be obtained. If the conditions are not met, the subsequent values ​​will also be obtained.

I wrote a piece of code using AI. Looking at the comments and actual results, it seems that it first obtains a request result, performs judgment, and then waits for all remaining result requests to be completed, and then performs conditional judgment.

It does not wait for the requested results to be obtained in sequence and then perform conditions.

Promise.all() will wait for all the results then do the judgment, and I think I should not use it.

    const promises = [];
    // List of links to request
    var urlavl = 'https://www.aaa.com'; 
    var urlero = 'https://www.bbb.com';
    var urlkuti = 'https://www.ccc.com';

    var urls = [urlwiki,urlero,urlkuti]; 
    // Traverse the linked list, initiate a request and store Promise and corresponding conditions
    urls.forEach((url) => {
        promises.push(new Promise((resolve, reject) => {
            GM_xmlhttpRequest({
                method: "GET",
                url: url,
                onload: function(response) {
                    let domNew = new DOMParser().parseFromString(response.responseText, 'text/html');
                    resolve(domNew);
                },
                onerror: function(error) {
                    resolve('ERROR' + url); 
                }
            });
        }));
    });
    Promise.race(promises)
        .then((data) => {
        // Determine whether the first completed request meets the corresponding conditions
        if(data.indexOf('#') > -1) {
            console.log(data);
            
            // Cancel other outstanding requests
            promises.forEach(promise => {
                if (promise.cancel) {
                    promise.cancel();
                }
            });
        } else {
            // console.log(`The link does not meet the conditions, waiting for other results...`);

            // Promise.all() Wait for all requests to complete
            Promise.all(promises)
                .then(results => {
                // Traverse all results and find results that meet the corresponding conditions
                console.log('Search again')
                results.forEach((data) => {
                    if(data.indexOf('#') > -1) {
                        console.log(data);
                    // console.log(`The link meets the conditions:`, response);
                    }
                });
            })
                .catch(error => {
                console.error("ERROR:", error);
            });
        }
    })
        .catch(error => {
        console.error("ERROR:", error);
    });

0

You do not even need promises, just sequential fetch

// List of links to request
const urls = ['https://www.aaa.com','https://www.bbb.com','https://www.ccc.com'];
let index = 0;

const process = (text,i) => console.log(`found # in ${urls[i]}`);
const getData = () => {
  if (index >= urls.length) {
    console.log('Did not find what you were looking for');
    return;
  }
  fetch(url[index])
  .then((response) => response.text())
  .then((text) => {
    if (text.includes('#')) {
      process(text,index); // finish
      return;
    }
    index++;
    setTimeout(getData,500); // give the server a rest
  }
}

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật