SOURCE

function myall(iterator) {
    return new Promise((resolve, reject) => {
        let len = 0
        let count = 0
        let reslist = []
        for (const p of iterator) {
            let resindex = len++
            Promise.resolve(p).then((res) => {
                reslist[resindex] = res
                if (++count === len) resolve(reslist)
            }).catch(reject)
        }
        if (len === 0) resolve(reslist)
    })
}

function deepcopy(target, hash = new WeakMap()) {
    const type = Object.prototype.toString.call(target).slice(8, -1)
    let res = null
    if (type === 'Object') res = {}
    else if (type === 'Array') res = []
    else return target
    if (hash.has(target)) return hash.get(target)
    hash.set(target, res)
    for (const key in target) {
        res[key] = deepcopy(target[key], hash)
    }
    return res
}

function asyncpool(poollimit, urls, request) {
    let ret = []
    let execution = []
    let i = 0
    function enqueue() {
        if (i === urls.length) return Promise.resolve()
        const url = urls[i++]
        const p = Promise.resolve(request(url))
        ret.push(p)
        execution.push(p)
        p.then(() => execution.splice(execution.indexOf(p), 1))
        let r = Promise.resolve()
        if (execution.length >= poollimit) r = Promise.race(execution)
        return r.then(() => enqueue())
    }
    return enqueue().then(() => Promise.all(ret))
}

function sort(nums) {
    if (nums.length <= 1) return nums
    let left = [], right = []
    const middleidx = Math.floor(nums.length / 2)
    const middle = nums.splice(middleidx, 1)[0]
    nums.forEach(item => {
        if (item < middle) left.push(item)
        else right.push(item)
    })
    return [...sort(left), middle, ...sort(right)]
}

function search(nums, target) {
    let left = 0, right = nums.length - 1
    while (left <= right) {
        let middle = Math.floor(left + (right - left) / 2)
        if (nums[middle] === target) return middle
        let issmail = nums[middle] < target
        left = issmail ? middle + 1 : left
        right = issmail ? right : middle - 1
    }
    return -1
}

class Event {
    _handlers = {}

    on(type, handler) {
        this._handlers[type] = this._handlers[type] || []
        this._handlers[type].push(handler)
    }
    emit(type, ...params) {
        if (!this._handlers[type]) throw 'err'
        this._handlers[type].forEach(handler => handler(...params))
    }
    off(type, handler) {
        if (!this._handlers[type]) throw 'err'
        if (!handler) delete this._handlers[type]
        else {
            let idx = this._handlers[type].indexOf(handler)
            if (idx === -1) throw 'err'
            this._handlers[type].splice(idx, 1)
            if (this._handlers[type].length === 0) delete this._handlers[type]
        }
    }
}
console 命令行工具 X clear

                    
>
console