Skip to content

Instantly share code, notes, and snippets.

@peterkarn
Created July 28, 2021 07:26
Show Gist options
  • Save peterkarn/4e170e5622efbab6717ce7fec4ae22dd to your computer and use it in GitHub Desktop.
Save peterkarn/4e170e5622efbab6717ce7fec4ae22dd to your computer and use it in GitHub Desktop.
const WRING_OUT_TIME = 250;
const SQUAT_TIME = 200;
function wringOut(count) {
return new Promise((resolve, reject) => {
if (count > 100) {
reject(new Error('Слишком много отжиманий'))
} else {
setTimeout(() => {
resolve();
}, count * WRING_OUT_TIME)
}
});
}
function squatting(count) {
return new Promise((resolve, reject) => {
if (count > 1000) {
reject(new Error('Слишком много приседаний'))
} else {
setTimeout(() => {
resolve();
}, count * SQUAT_TIME)
}
});
}
// без async
// console.log('Начать тренировку');
// wringOut(3)
// .then(() => { //в then передается параметр из resolve
// console.log('3 отжиманий');
// return squatting(5554)
// })
// .then(() => {
// console.log('5 приседаний');
// })
// .catch((x) => { //в catch передается параметр из reject
// console.log(x.toString());
// })
// async await
// async function myTraining() { //async по умолчанию возвращает promise
// console.log('Начать тренировку');
// try {
// await wringOut(5);
// console.log('3 отжиманий');
// await squatting(3);
// console.log('5 приседаний');
// return true; // возврат результата тренировки
// } catch (error) {
// console.log(error.toString());
// return false; // возврат результата тренировки
// }
// }
// myTraining().then((result) => {
// console.log(result);
// })
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment