infinnie / infinnie.github.io

https://infinnie.github.io/
Apache License 2.0
0 stars 1 forks source link

Promises and proxies #5

Open infinnie opened 7 years ago

infinnie commented 7 years ago

As such, it was decided that the way to recognize a Promise (or something that behaves like a Promise) would be to define something called a "thenable" as any object or function which has a then(..) method on it. It is assumed that any such value is a Promise-conforming thenable.

https://github.com/getify/You-Dont-Know-JS/blob/master/async%20%26%20performance/ch3.md

var myProxy = new Proxy({}, {
    get: function (tar, name) {
        if (name === "then"){
            return function () { console.log(1); };
        }
        return tar[name];
    }
});
console.log("then" in myProxy); // false
Promise.resolve(myProxy).then(function () {
    console.log(2);
}); // 1 is logged