куе-уе

var queue = function({max_concurrency = 1} = {})
{
    var queue = []
    var pending = 0
    var iterate_queue = function()
    {
        if((queue.length > 0) && (pending < max_concurrency))
        {
            pending++
            var item = queue.shift()
            item.fn(item.done)
        }
    }
    var interface = 
    {
        add: function(fn)
        {
            return new Promise(function(resolve,reject)
            {
                queue.push(
                {
                    fn: fn,
                    done: function(error,result)
                    {
                        pending--
                        setTimeout(iterate_queue)
                        if(error) 
                        { 
                            reject(error)
                        }
                        else
                        {
                            resolve(result)
                        }
                    }
                })
                iterate_queue()
            })
        }
    }
    return interface
}
[ ]
 

Leave a Reply

*