×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: Javascript
Posted by: Maria Dan
Added: Jun 11, 2021 4:35 PM
Modified: Jun 16, 2021 4:41 PM
Views: 3858
Tags: no tags
  1. let timeout = () => new Promise (resolve => setTimeout(() => resolve('done'), 3000));
  2.  
  3. timeout()
  4.   .then((str) => {
  5.     console.log(str)
  6.     return timeout()
  7.     })
  8.   .then((str) => {
  9.     console.log(str)
  10.     return timeout()
  11.     })
  12.   .then((str) => {
  13.     console.log(str)
  14.     return timeout()
  15.     })
  16.  
  17.  
  18. let a = 1;
  19. let test = new Promise (function(resolve, reject) {
  20.  
  21.   if(a === 1) {
  22.     resolve('good');
  23.   } else {
  24.     reject('bad')
  25.   }
  26. });
  27.  test
  28.  .then((str) => console.log(str))
  29. .catch((str) => console.log(str))
  30.  
  31. const random = (min, max) => Math.floor(Math.random() * (max - min + 1) + min)
  32.  
  33. let promise = new Promise(function (resolve, reject) {
  34.  
  35.  let assignNewValueForA = () =>  {
  36.    
  37.    let result = random(1, 10);
  38.  
  39.    if(result <= 5) {
  40.      // сработает в .then()
  41.      resolve();
  42.    } else {
  43.      // сработает в .catch()
  44.      reject();
  45.    }
  46.  
  47.  }
  48.  // ждем 2 секунды
  49.  setTimeout(assignNewValueForA, 2000);
  50. });
  51.  
  52. promise(url)
  53.  .then(() => console.log('you win'))
  54.  .catch(() => console.log('you loose'))