You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
functionisObject(value){vartype=typeofvalue;returnvalue!=null&&(type=="object"||type=="function");}/** * form vue * Check if two values are loosely equal - that is, * if they are plain objects, do they have the same shape? */functionlooseEqual(a,b){if(a===b)returntrue;constisObjectA=isObject(a);constisObjectB=isObject(b);if(isObjectA&&isObjectB){try{constisArrayA=Array.isArray(a);constisArrayB=Array.isArray(b);if(isArrayA&&isArrayB){return(a.length===b.length&&a.every((e,i)=>{returnlooseEqual(e,b[i]);}));}elseif(ainstanceofDate&&binstanceofDate){returna.getTime()===b.getTime();}elseif(!isArrayA&&!isArrayB){constkeysA=Object.keys(a);constkeysB=Object.keys(b);return(keysA.length===keysB.length&&keysA.every((key)=>{returnlooseEqual(a[key],b[key]);}));}else{returnfalse;}}catch(e){returnfalse;}}elseif(!isObjectA&&!isObjectB){// 判断基本类型 number、string、boolean、null、undefined、Symbol、BigIntreturnString(a)===String(b);}else{returnfalse;}}consta={weight: 8,fields: [{name: "全额缴费",value: 0,},{name: "按月缴费",value: 1,},],};constb={weight: "8",fields: [{name: "全额缴费",value: 0,},{name: "按月缴费",value: 1,},],};console.log(looseEqual(a,b));
constp=()=>newPromise((resolve,reject)=>setTimeout(()=>{leta=Math.random();letflag=a>0.1 ? 0 : 1;console.log(flag);flag===1 ? resolve(flag) : reject(flag);},1000));/* Promise.retry = function (promiseFn, times = 3) { return new Promise(async (resolve, reject) => { while (times--) { try { let ret = await promiseFn(); resolve(ret); break; } catch (error) { if (!times) reject(error); } } }); }; *//* Promise.retry = function (promiseFn, times = 3) { return new Promise((resolve, reject) => { let count = 0; let action = function () { promiseFn() .then(resolve) .catch((err) => { count++; if (count >= times) { reject(err); } else { action(); } }); }; action(); }); }; */Promise.retry=function(asyncFn,times=3){letcount=0;functionexecuteFn(){returnnewPromise((resolve,reject)=>{resolve(asyncFn());}).then((res)=>{returnPromise.resolve(res);}).catch((err)=>{count++;if(count>=times){returnPromise.reject(err);}else{returnexecuteFn();}});}returnexecuteFn();};Promise.retry(p,3).then(()=>{console.log("okkk");});
任务中断
批量执行异步任务,有任务返回 false 就中断执行并返回结果。
letasyncFn=(val)=>{returnnewPromise((resolve)=>{setInterval(()=>{resolve(val);},1000);});};lettasks=[true,false,false].map((v)=>()=>asyncFn(v));// 基础 for 循环/* async function run() { for (let i = 0; i < tasks.length; i++) { const task = tasks[i]; let res = await task(); if (!res) { return false; } } return true; } */// for ofasyncfunctionrun(){for(consttaskoftasks){letres=awaittask();if(!res){returnfalse;}}returntrue;}asyncfunctionmain(){letresult=awaitrun();console.log("result",result);}main();// result false
工具方法
load
加减乘除
深拷贝
对象全等 looseEqual
对象取键 get
merge
枚举
队列
栈
函数式
函数记忆
升级版
函数柯里化
函数管道 compose、pipe
算法
找出现最多次数的字母
二分查找
大数相加
玩转异步
基础模拟 Promise Class
await 错误捕获
Promise 同步与异步
调试
异步重试
按时间重试
按次数重试
实现
Promie.retry
,成功后 resolve 结果,失败后重试,尝试超过一定次数才真正 reject任务中断
批量执行异步任务,有任务返回 false 就中断执行并返回结果。
其他
reduce 经典用法
padStart 方法重写
Proxy api
动态导入
The text was updated successfully, but these errors were encountered: