Skip to content

Instantly share code, notes, and snippets.

View gskachkov's full-sized avatar

Oleksander Skachkov gskachkov

  • Itera Consulting
  • Ukraine, Kiev
View GitHub Profile
const promise = new Promise(resolve => {
setTimeout(()=>resolve('success'));
});
const startOperation = () => { console.log('start'); };
const finishOperation = () => { console.log('finish'); };
startOperation();
promise
const promise = new Promise(resolve => {
setTimeout(()=>resolve('success'));
});
const startOperation = () => { console.log('start'); };
const finishOperation = () => { console.log('finish'); };
startOperation();
promise
Promise.reject('error #1')
.then(value => console.log('level-1:', value), error => console.log('level-1-error:', error))
.catch(error => console.log('error in catch:', error))
.finally(() => console.log('finally'))
.then(value => console.log('level-2:', value), error => console.log('level-2-error:', value));
// level-1-error: error #1
// finally
// level-2: undefined
Promise.reject('reject')
.finally(() => console.log('finally#1'))
.catch(error => console.log('catch#1', error))
.catch(error => console.log('catch#2', error));
//finally#1
//catch#1 reject
Promise.resolve('resolve')
.then(result=> { console.log('then#1', result); return Promise.resolve('next-step'); })
.finally(() => console.log('finally#1'))
var weaver = function (target, methodName) {
var before = function () {
console.log(arguments);
};
var old = target[methodName];
target[methodName] = function () {
before.apply(this, arguments);
return old.apply(this, arguments);
};
var proxiedObject = new Proxy(target, handler);
const handler = {
defineProperty: function(target, property, descriptor) {
if (property.charAt(0) === "_") return false;
return Reflect.defineProperty(target, property, descriptor);
}
};
const proxy = new Proxy({}, handler);
proxy.id = "ABCD"; // { id: 'ABCD' }
const handler = {
get: function(target, property, receiver) {
if (property.startAt(0) === "_") return undefined;
return target[property];
}
};
const proxy = new Proxy({}, handler);
console.log(proxy.id); // 'ABCD'
class A {
constructor (value) { this.value = value; }
getValue() { return this.value; }
}
const proxiedClass = new Proxy(A, {
construct: function(target, [ first ], newTarget) {
return new target(first + ":proxy-value");
}
});
const foo = (a, b, c) => `${a}:${b}:${c}`;
const proxy = new Proxy(foo, {
apply: function(target, thisArg, argumentsList) {
return target.apply(thisArg, argumentsList);
}
});
console.log(proxy("1", "2", "3")); // 1:2:3