延时循环数组 DelayLoops

在操作mongodb数据库时,查询了数据得到一个数组,然后还得遍历这个数组,再对每个数组的值进行数据库相关的增删改查,

如果单纯的遍历会出错。所以才写了这个简单的类。

var a = [1,2,3,4,5];
DelayLoops(a, function (item, index){
  setTimeout(() => {
    console.log(this, item, index)
    this.proceed();    //继续进行遍历
  }, 500)
})
.then(function (){
  console.log('finish');
})
.then(function (){
  console.log('finish again');
});

控制台显示:

github: https://github.com/SunGg12138/DelayLoops

原文地址:https://www.cnblogs.com/sungg/p/6516836.html