This file contains hidden or 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
| const getActiveUsers = async () => { | |
| try { | |
| const response = await fetch('https://api.since1979.dev/users'); | |
| if (!response.ok) throw new Error('Network response was not ok'); | |
| const users = await response.json(); | |
| // Now we use our Array Methods to process the data! |
This file contains hidden or 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
| headers: { | |
| 'Content-Type': 'application/json', | |
| 'Accept': 'application/json', | |
| 'X-CSRF-TOKEN': document.querySelector('meta[name="csrf-token"]').getAttribute('content') | |
| } | |
This file contains hidden or 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
| const createNewUser = async (userData) => { | |
| try { | |
| const response = await fetch('https://api.since1979.dev/users', { | |
| method: 'POST', | |
| headers: { | |
| 'Content-Type': 'application/json', | |
| 'Accept': 'application/json' | |
| }, | |
| body: JSON.stringify(userData) |
This file contains hidden or 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
| if (!response.ok) { | |
| const errorBody = await response.json(); | |
| throw new Error(errorBody.message || 'Something went wrong on the server'); | |
| } | |
This file contains hidden or 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
| const getSecureData = async (url) => { | |
| try { | |
| const response = await fetch(url); | |
| // This is the missing piece! | |
| // If the server returns 404 or 500, we jump to the catch block manually. | |
| if (!response.ok) { | |
| throw new Error(`HTTP error! status: ${response.status}`); | |
| } |
This file contains hidden or 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
| // 1. Mark the function as async | |
| const fetchStarTrekData = async () => { | |
| // 2. The first 'await' waits for the initial response (headers) | |
| const response = await fetch('https://api.since1979.dev/characters'); | |
| // 3. The second 'await' waits for the full body to be parsed as JSON | |
| const data = await response.json(); | |
| // Now the data is ready to be used |
This file contains hidden or 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
| const getUserData = async () => { | |
| try { | |
| const userResponse = await fetch('https://api.example.com/user/1'); | |
| const user = await userResponse.json(); | |
| const postsResponse = await fetch(`https://api.example.com/posts?userId=${user.id}`); | |
| const posts = await postsResponse.json(); | |
| console.log(`User: ${user.name}, Posts: ${posts.length}`); |
This file contains hidden or 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 works, but it's hard to follow as it grows | |
| fetch('https://api.example.com/user/1') | |
| .then(response => response.json()) | |
| .then(user => { | |
| console.log('User found:', user.name); | |
| return fetch(`https://api.example.com/posts?userId=${user.id}`); | |
| }) | |
| .then(response => response.json()) | |
| .then(posts => { |
This file contains hidden or 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
| document.addEventListener('wpcf7mailsent', function(event) { | |
| gtag('event', 'form_submission', { | |
| 'form_id': event.detail.contactFormId, | |
| 'event_category': 'Engagement' | |
| }); | |
| }, false); | |
This file contains hidden or 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
| // Track Telephone Clicks | |
| const phoneLinks = document.querySelectorAll('a[href^="tel:"]'); | |
| phoneLinks.forEach(link => { | |
| link.addEventListener('click', () => { | |
| gtag('event', 'contact_click', { | |
| 'type': 'phone', | |
| 'number': link.href | |
| }); | |
| }, { once: true }); |
NewerOlder