Sebastian Meier Sebastian Meier - 1 year ago 121
Javascript Question

stack overflow when returning an ES6 proxy through a promise

I'm trying to intercept a method call on a ES6 proxy to be able to do stuff in between with the information I get from the proxy. Now in my case there is quite some stuff going on before creating and returning the proxy from some kind of factory. Because of all this stuff I decided to wrap the prerequisites into a promise-function so that I can chain the proxy creation right onto it and return the resulting proxy through the promise chain. Here's the code to reproduce the problem:


'use strict';

// require('harmony-reflect');

class ProxyFactory {

create(options) {

const self = this;

const handler = {

get(target, propertyKey, receiver) {

if (propertyKey === 'then') {

return function proxyPromiseWrapper(thenCallback) {
const innerProxy = self.create(options);
return thenCallback(innerProxy);

return function resourceFunctionProxy() {

const callContext = {
target: target,
method: propertyKey,
args: arguments,
thisContext: this

const resourceInstanceMethod = Reflect.get(options.originalObject, callContext.method);
return resourceInstanceMethod.apply(callContext.thisContext, callContext.arguments);


return new Proxy(options.originalObject, handler);


module.exports = ProxyFactory;


'use strict';

const Promise = require('bluebird');
const ProxyFactory = require('./proxy_factory.min.js');

const proxyFactory = new ProxyFactory();

function createProxyWithPromise() {

const TestClass = class {
doSomething() {
return Promise.resolve('promise return value');

const options = {
originalObject: new TestClass()

return Promise.resolve()
.then(() => {
return proxyFactory.create(options);

function test() {

.then((proxy) => {

const result = proxy.doSomething();

console.log(result); // should output 'promisereturnvalue'


Before doSomething() is called on the proxy the then()-function is called over and over again, resulting in a stack overflow.
I already asked this question in the node.js github issues and you can find the previous conversation here:
Maybe it helps someone helping me ;)

Answer Source

Your problem is that your proxy always returns a function for accesses on any property, including then. That will make promises implementations treat it as a thenable, trying to to resolve it - where your code went horribly wrong. But you should fix the root of the issue:

get (target, propertyKey, receiver) {
    if (!(propertyKey in target))
        return undefined;
    else if (typeof target[propertyKey] != "function")
        return …;
        return function resourceFunctionProxy() {
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download