Skip to content

限制异步操作并发数并尽可能快地完成 #32

@Rain120

Description

@Rain120

同时下载的链接不超过 3 个的情况下,尽可能快地完成。

const urls = [
    {
        time: 1000,
        href: 'link1',
    },
    {
        time: 2000,
        href: 'link2',
    },
    {
        time: 3000,
        href: 'link3',
    },
    {
        time: 4000,
        href: 'link4',
    },
    {
        time: 3000,
        href: 'link5',
    },
    {
        time: 2000,
        href: 'link6',
    },
    {
        time: 1000,
        href: 'link7',
    },
    {
        time: 3000,
        href: 'link8',
    },
    {
        time: 1000,
        href: 'link9',
    },
    {
        time: 1000,
        href: 'link10',
    },
];

function loadImg(url) {
    return new Promise((resolve, reject) => {
        console.log('----' + url.href + 'start!');
        setTimeout(() => {
            console.log(url.href + 'ok!');
            resolve();
        }, url.time);
    });
}

function limitLoad(list, callback, limit) {
  // ...
}

limitLoad(urls, loadImg, 3);

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions