Suggest a feature
×

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
Views: 230
Tags: no tags
  1. let a = 1;
  2. let test = new Promise (function(resolve, reject) {
  3.  
  4.   if(a === 1) {
  5.     resolve('good');
  6.   } else {
  7.     reject('bad')
  8.   }
  9. });
  10.  test
  11.  .then((str) => console.log(str))
  12. .catch((str) => console.log(str))
  13.  
  14. const random = (min, max) => Math.floor(Math.random() * (max - min + 1) + min)
  15.  
  16. let promise = new Promise(function (resolve, reject) {
  17.  
  18.  let assignNewValueForA = () =>  {
  19.    
  20.    let result = random(1, 10);
  21.  
  22.    if(result <= 5) {
  23.      // сработает в .then()
  24.      resolve();
  25.    } else {
  26.      // сработает в .catch()
  27.      reject();
  28.    }
  29.  
  30.  }
  31.  // ждем 2 секунды
  32.  setTimeout(assignNewValueForA, 2000);
  33. });
  34.  
  35. promise(url)
  36.  .then(() => console.log('you win'))
  37.  .catch(() => console.log('you loose'))