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 promise = new Promise(resolve => { | |
setTimeout(()=>resolve('success')); | |
}); | |
const startOperation = () => { console.log('start'); }; | |
const finishOperation = () => { console.log('finish'); }; | |
startOperation(); | |
promise |
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 promise = new Promise(resolve => { | |
setTimeout(()=>resolve('success')); | |
}); | |
const startOperation = () => { console.log('start'); }; | |
const finishOperation = () => { console.log('finish'); }; | |
startOperation(); | |
promise |
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
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 |
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
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')) |
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
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); | |
}; |
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
var proxiedObject = new Proxy(target, handler); |
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 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' } |
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 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' |
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
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"); | |
} | |
}); |
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 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 |