Advertisement
Guest User

Untitled

a guest
Aug 20th, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.50 KB | None | 0 0
  1. const arr = [1,2,3,4];
  2.  
  3. Array.prototype.delayedForEach = function(callback, timeout, thisArg){
  4. return new Promise((reject, resolve) => {
  5. var i = 0,
  6. l = this.length,
  7. self = this,
  8. caller = function(){
  9. callback.call(thisArg || self, self[i], i, self);
  10. if (++i < l) {
  11. setTimeout(caller, timeout)
  12. } else {
  13. setTimeout(resolve, timeout);
  14. }
  15. };
  16. caller();
  17. });
  18. };
  19.  
  20. await arr.delayedForEach(async (elem, index) => {
  21. await console.log(elem);
  22. }, 2000);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement