-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9b8ca2b
commit a9f0bfa
Showing
6 changed files
with
269 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
|
||
<head> | ||
<meta charset="UTF-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<title>Document</title> | ||
<script src="./index.js" defer></script> | ||
</head> | ||
|
||
<body> | ||
|
||
</body> | ||
|
||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
|
||
// -------- | ||
|
||
// работа с json - преобразование строки в объект js | ||
// const data = JSON.parse(`{ | ||
// "type": "general", | ||
// "setup": "What kind of award did the dentist receive?", | ||
// "punchline": "A little plaque.", | ||
// "id": 255 | ||
// } | ||
// `); | ||
|
||
|
||
// объект js преобразовали в json строку | ||
// const pers = { | ||
// name: 'bob', | ||
// age: 22 | ||
// } | ||
|
||
// const persJsonStr = JSON.stringify(pers); | ||
|
||
// console.log(persJsonStr); | ||
|
||
|
||
// -------------- | ||
|
||
// запрос к паблик апи random_joke ( по умолчанию метод GEt ) | ||
// fetch('https://official-joke-api.appspot.com/random_joke') | ||
// .then(response => { | ||
// return response.json(); | ||
// }).then(data => { | ||
// console.log(data); | ||
// }); | ||
|
||
// обращение к гитхаб репозиторию | ||
// let url1 = 'https://api.github.com/repos/SergeyRazin2014/ait-frontend/commits'; | ||
let url1 = 'https://catfact.ninja/fact'; | ||
|
||
// fetch(url1) | ||
// .then(response => { | ||
// return response.json(); | ||
// }).then(data => { | ||
// console.log(data); | ||
// }); | ||
|
||
|
||
// const loadDagta = ()=>{ | ||
// fetch(url1) | ||
// .then(response => { | ||
// return response.json(); | ||
// }).then(data => { | ||
// console.log(data); | ||
// }); | ||
// } | ||
|
||
// loadDagta(); | ||
|
||
|
||
|
||
|
||
|
||
const data = `{ | ||
"type": "general", | ||
"setup": "What kind of award did the dentist receive?", | ||
"punchline": "A little plaque.", | ||
"id": 255, | ||
file: 'xcas2435gqsdagdfasdfasdfasdf' | ||
}`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"type": "general", | ||
"setup": "How do you make a hankie dance?", | ||
"punchline": "Put a little boogie in it.", | ||
"id": 128 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
|
||
<head> | ||
<meta charset="UTF-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<title>Document</title> | ||
<script src="./index.js"></script> | ||
</head> | ||
|
||
<body> | ||
|
||
</body> | ||
|
||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
|
||
// пример функции с ключевым словом async (возвращает промис) | ||
// const foo = async () => { | ||
// return 'hello'; | ||
// } | ||
|
||
// // вызов async функции | ||
// foo() | ||
// .then(data => { | ||
// console.log(data); | ||
// }) | ||
|
||
// ----------------- | ||
|
||
// пример с ключевым словом await | ||
// const foo = async () => { | ||
// const p = new Promise((resolve, reject) => { | ||
// setTimeout(() => { | ||
// resolve('hello'); | ||
// }, 1000); | ||
|
||
// }); | ||
|
||
// // ждем выполнение промиса | ||
// const res = await p; | ||
|
||
// console.log(res); | ||
// } | ||
|
||
// foo(); | ||
|
||
// ---------------- | ||
|
||
// const p = new Promise((resolve, reject) => { | ||
// setTimeout(() => { | ||
// resolve('hello'); | ||
// }, 1000); | ||
|
||
// }); | ||
|
||
// // ОШИБКА SyntaxError: await is only valid in async functions and the top level bodies of modules | ||
// const res = await p; | ||
|
||
// -------------- | ||
|
||
// пример классической фнукции с ключ словом async | ||
// async function foo() { | ||
// const p = new Promise((resolve, reject) => { | ||
// setTimeout(() => { | ||
// resolve('hello'); | ||
// }, 1000); | ||
|
||
// }); | ||
|
||
// const res = await p; | ||
// console.log(res); | ||
// } | ||
|
||
// foo(); | ||
|
||
// --------------- | ||
|
||
// | ||
// const foo = async () => { | ||
// const p = new Promise((resolve, reject) => { | ||
// setTimeout(() => { | ||
// resolve('hello'); | ||
// }, 1000); | ||
|
||
// }); | ||
|
||
// // ждем выполнение промиса | ||
// const res = await p; | ||
|
||
// return res; | ||
// } | ||
|
||
// // результат вызова asycn функции будет промисом не смотря на то что внутри нее вызывается await и возвращается примитивный результат | ||
// const res = foo(); | ||
|
||
// ---------------- | ||
|
||
const loadDataAsync = async () => { | ||
const promise1 = new Promise((resolve, reject) => { | ||
setTimeout(() => resolve('Данные'), 1000); | ||
}); | ||
|
||
return promise1; | ||
} | ||
|
||
const analyseAsync = async (data) => { | ||
const promise1 = new Promise((resolve, reject) => { | ||
console.log(data); | ||
setTimeout(() => resolve('Проанализированные данные '), 1000); | ||
}); | ||
|
||
return promise1; | ||
} | ||
|
||
const saveDataAsync = async (data) => { | ||
const promise1 = new Promise((resolve, reject) => { | ||
console.log(data); | ||
setTimeout(() => resolve('Сохраненные сохранены '), 1000); | ||
}); | ||
return promise1; | ||
} | ||
|
||
// вызов через then | ||
// const main = () => { | ||
// loadDataAsync() | ||
// .then(data => { | ||
// return analyseAsync(data); | ||
// }).then(data => { | ||
// return saveDataAsync(data); | ||
// }).then(data => { | ||
// console.log(data); | ||
// }); | ||
// } | ||
// main(); | ||
|
||
|
||
// вызов асинхронных функция похоже на синхронынй код | ||
// const main = async () => { | ||
// const data1 = await loadDataAsync(); | ||
// const data2 = await analyseAsync(data1); | ||
// const data3 = await saveDataAsync(data2); | ||
// console.log(data3); | ||
// } | ||
|
||
// main(); | ||
|
||
// ----------- | ||
|
||
const fooAsync = async () => { | ||
|
||
const promise1 = new Promise((resolve, reject) => { | ||
// вызываем reject на промисе | ||
setTimeout(() => reject('ERROR111'), 1000); | ||
}); | ||
|
||
// если промис реджектит что-то - то await - выбросит исключение ( тут будет ошибка ) | ||
// const result = await promise1; | ||
|
||
// чтобы обработать исключение нужно блок с await - завернить в try catch | ||
try { | ||
const result = await promise1; | ||
console.log(result); | ||
} catch (err) { | ||
console.log('Произошла ошибка'); | ||
} | ||
} | ||
|
||
|
||
|