function c(z2) {
    console.log(new Error().stack);
}
function b(z1) {
    c(z1+ 1);
}
function a(z) {
    b(z + 1);
}
a(1);

---

var xhr = new XMLHttpRequest();
xhr.open('GET', 'http://babeljs.io', true);
xhr.onload = function(e) {
  if (this.status == 200) {
    console.log("Działa");
  }
};
xhr.send();

---

fs.readFile('/etc/passwd',
  // powodzenie
  function(data) {
    console.log(data)
  },
  // błąd
  function(error) {
    console.log(error)
  }
);

---

asyncFunction(arg)
.then(resultA=>{
  //...
  return asyncFunctionB(argB);
})
.then(resultB=>{
  //...
})

---

fs.readFile('text.json',
  function (error, text) {
      if (error) {
          console.error('Błąd w trakcie odczytu pliku tekstowego');
      } else {
          try {
              //...
          } catch (e) {
              console.error('Nieprawidłowa zawartość');
          }
      }
});

---

const p = new Promise(
  function (resolve, reject) { // (1)
      if ( ) {
          resolve(value); // powodzenie
      } else {
          reject(reason); // niepowodzenie
      }
});
---

import {readFile} from 'fs';
function readFileWithPromises(filename) {
    return new Promise(
        function (resolve, reject) {
            readFile(filename,
                (error, data) => {
                    if (error) {
                        reject(error);
                    } else {
                        resolve(data);
                    }
                });
        });
}

---

Promise.all([
    f1(),
    f2()
])
.then(([r1,r2]) => {
    //...
})
.catch(error => {
    //...
});

---

const introspection = {
  intro() {
    console.log("Myślę, więc jestem");
  }
}
for (const key of Object.keys(introspection)){ 
  console.log(key); // intro
}
---

var handler = {
  get: function(target, name){
    return name in target ? target[name] :42;
  }
}
var p = new Proxy({}, handler);
p.a = 100;
p.b = undefined;
console.log(p.a, p.b);      
console.log('c' in p, p.c); 

---

let ageValidator = {
  set: function(obj, prop, value) {
    if (prop === 'age') {
      if (!Number.isInteger(value)) {
        throw new TypeError('Wiek nie jest liczbą');
      }
      if (value > 100) {
        throw new RangeError('Nie możesz być starszy niż 100');
      }
    }
    // Jeśli nie ma błędu, po prostu zapisujemy wartość we właściwości
    obj[prop] = value;
  }
};
let p = new Proxy({}, ageValidator);
p.age = 100;
console.log(p.age); 
p.age = 'Dwa';      
p.age = 300;        

---

var car = {
  name: "Ford",
  method_1: function(text){
    console.log("method_1 wywołana z parametrem "+ text);
  }
}
var methodInterceptorProxy = new Proxy(car, {
  // cel to obiekt, dla którego stosowane jest proxy, a odbiorcą jest proxy
  get: function(target, propKey, receiver){
    // chcemy przechwycić tylko wywołania metod, a nie dostęp do właściwości
    var propValue = target[propKey];
    if (typeof propValue != "function"){
      return propValue;
}
    else{
      return function(){
        console.log("przechwytywanie wywołania " + propKey + " w samochodzie " + target.name);
        // cel to obiekt, dla którego stosowane jest proxy
        return propValue.apply(target, arguments);
      }
    }
  }
});
methodInterceptorProxy.method_1("Mercedes");

---


