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
if(!Array.prototype.forEach){Array.prototype.forEach=function(callback,thisArg){varT,k;if(this==null){thrownewTypeError(' this is null or not defined');}varO=Object(this);varlen=O.length>>>0;if(typeofcallback!=="function"){thrownewTypeError(callback+' is not a function');}if(arguments.length>1){T=thisArg;}k=0;while(k<len){varkValue;if(kinO){kValue=O[k];callback.call(T,kValue,k,O);}k++;}};}
Activity
sisterAn commentedon Jun 21, 2020
1s后同时打印 1、4、9原因:使用
promise或async函数作为forEach()等类似方法的callback参数并不会等待异步的执行如果我们希望每隔
1s输出一个结果,方法有:for循环for…of/for…in循环promise的链式调用解法一:for 循环
解法二:for…in / for…of
for...infor...of解法三:利用 promise 的链式调用
7777sea commentedon Jun 22, 2020
运行结果:一秒后输出1,4,9
原因:在forEach里面写的callback函数会直接在while循环里面调用
改造:用for...of 或者for循环代替 forEach
贴一下forEach的源码
knightgao commentedon Jun 22, 2020
运行结果,直接输出1 4 9
原因:是同步代码,直接执行了
代码:
遍历异步的循环
xyj626553989 commentedon Jun 22, 2020
运行结果,直接输出1 4 9
原因:用了递归调用
代码:
z253573760 commentedon Feb 2, 2021