cscty
cscty
class scheduler { constructor(max) { this.max = max; // 当前并发数 this.count = 0; // 阻塞队列 this.queue = []; } async add(fn) { // 判断并发数是否大于最大数 if (this.count >= this.max) { //...
let tasks = []; for (let i = 0; i < 8; i++) { tasks.push(() => { return new Promise((resolve, reject) => { setTimeout(() => { resolve(i); console.log(i); }, 1000);...
let tasks = []; for (let i = 0; i < 8; i++) { tasks.push(() => { return new Promise((resolve, reject) => { setTimeout(() => { resolve(i); console.log(i); }, 1000);...
class math { constructor(value = 0) { this.value = value; } add(...args) { this.value = args.reduce( (ljq, cur) => ljq + cur, this.value ); return this; } minus(...args) { this.value...
class Event { constructor() { // key为事件名,value为事件数组 this.events = {}; } on(key, fn) { if (this.events[key]) this.events[key].push(fn); else this.events[key] = [fn]; } emit(key, ...args) { if (!this.events[key]) return; this.events[key].forEach((fn) =>...
function timeout(promise, delay) { let p = new Promise((resolve, reject) => { setTimeout(() => { reject(); }, delay); }); return Promise.race([promise, p]); }
function treeToList(data) { let res = []; function dfs(data) { for (let i = 0; i < data.length; i++) { res.push({ id: data[i].id, text: data[i].text, parentId: data[i].parentId, }); if (data[i].children)...
``` Function.prototype.call = function (context, ...args) { context.fn = this; return context.fn(...args); }; Function.prototype.apply = function (context, args) { context.fn = this; return context.fn(...args); }; Function.prototype.bind = function (context, ...args)...
// 实现 Promise.retry,成功后 resolve 结果,失败后重试,尝试超过一定次数才真正的 reject Promise.retry = function (promiseFunc, num = 1) { return promiseFunc().then(undefined, (err) => { if (num > 0) { num--; return Promise.retry(promiseFunc, num); } return...
var numIslands = function (grid) { let row = grid.length; let col = grid[0].length; let used = new Array(row) .fill(0) .map(() => new Array(col).fill(0)); console.log(used); let count = 0; for...