Skip to content

Instantly share code, notes, and snippets.

View emre-edu-tech's full-sized avatar

Media Pons emre-edu-tech

View GitHub Profile
@emre-edu-tech
emre-edu-tech / basic-promise.js
Last active October 13, 2024 07:19
This is a simple demonstration of how then()-catch() methods work while heading an api. We can see clearly then() methods are called after the previous promise resolve.
// Here every then() method returns a new promise
// then() method always yields a promise after being executed, this way chaining is possible
// We can say here that every then() methods waits the previous promise to resolve
// Error handling is also easy. If any prior promise fails, catch() method will be triggered
fetch('https://api.openweathermap.org/data/2.5/weather?q=London,uk')
.then(response => {
return response.json();
})
.then(data => {
console.log(data);
@emre-edu-tech
emre-edu-tech / scope-hoisting.js
Created September 1, 2020 06:21
This example shows the importance of Javascript hoisting.
let result = 1;
// here this declaration must come first before using it
// it is a function variable declaration so it should be declared before its usage
const addNumber = function (numToAdd) {
result = result + numToAdd;
return result;
}
@emre-edu-tech
emre-edu-tech / functions.php
Last active October 13, 2024 07:29
Check the user's country and show them as a warning using SweetAlert2. Warning was shown on Checkout page if the country is other than Germany in this example.
<?php
function get_user_geo_country(){
if(is_checkout()) {
$geo = new WC_Geolocation(); // Get WC_Geolocation instance object
$user_ip = $geo->get_ip_address(); // Get user IP
$user_geo = $geo->geolocate_ip( $user_ip ); // Get geolocated user data.
$country = $user_geo['country']; // Get the country code
if($country != 'DE') {
?>
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@9"></script>