通过承诺返回ES6代理时,堆栈溢出 [英] stack overflow when returning an ES6 proxy through a promise

查看:168
本文介绍了通过承诺返回ES6代理时,堆栈溢出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图拦截一个ES6代理的方法调用,以便能够从代理获取的信息之间做一些事情。现在在我的情况下,在从某种工厂创建和返回代理之前,有很多事情发生。由于所有这些东西,我决定将前提条件包装到承诺函数中,以便可以将代理创建权连接到它上,并通过承诺链返回生成的代理。以下是重现问题的代码:

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:

proxy_factory.min.js

proxy_factory.min.js

'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;

test.js

'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() {

  createProxyWithPromise()
    .then((proxy) => {

      const result = proxy.doSomething();

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

test();



,导致堆栈溢出。
我已经在node.js github问题中提出了这个问题,你可以在这里找到前面的对话: https://github.com/nodejs/node/issues/8082
也许它有助于帮助我;)

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: https://github.com/nodejs/node/issues/8082 Maybe it helps someone helping me ;)

推荐答案

您的问题是您的代理总是返回一个函数来访问任何属性,包括然后。这将使承诺实现将其视为一种可行的方法,试图解决它 - 您的代码在哪里可怕的错误。但是您应该解决问题的根源:

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 …;
    else
        return function resourceFunctionProxy() {
            …

这篇关于通过承诺返回ES6代理时,堆栈溢出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆