Advertisement
Igor150195

Промисы

Aug 15th, 2022 (edited)
1,096
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. let myArray = [1,3,23,5,6,7,8,8];
  2. let myCounter = 1;
  3.  
  4. function myPromise() {
  5.     return new Promise((resolve, reject) => {
  6.  
  7.         try {
  8.  
  9.  
  10.             fetch('https://test-site.ru')
  11.             .then((response) => {
  12.                 return response.text();
  13.             })
  14.             .then((data) => {
  15.                 resolve(myCounter+=1);
  16.             });
  17.  
  18.  
  19.         }
  20.         catch (err) {
  21.             reject(err);
  22.         }
  23.     });
  24. };
  25.  
  26. function testPromise() {
  27.     myPromise().then((myCounter) => {
  28.  
  29.         if (myArray.length>myCounter) {
  30.             setTimeout(function(){
  31.                 testPromise();
  32.             }, 500)
  33.         };
  34.     });
  35. };
  36.  
  37. testPromise();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement