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