它是不好的做法有一个构造函数返回一个承诺? [英] Is it bad practice to have a constructor function return a Promise?

查看:127
本文介绍了它是不好的做法有一个构造函数返回一个承诺?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建一个博客平台的构造,它具有很多异步操作里面发生。这些范围从抓从目录中的帖子,把它们解析,通过模板引擎发送它们,等等。

I'm trying to create a constructor for a blogging platform and it has many async operations going on inside. These range from grabbing the posts from directories, parsing them, sending them through template engines, etc.

所以我的问题是,这将是不明智的,有我的构造函数返回,而不是他们叫反对

So my question is, would it be unwise to have my constructor function return a promise instead of an object of the function they called new against.

例如:

var engine = new Engine({path: '/path/to/posts'}).then(function (eng) {
   // allow user to interact with the newly created engine object inside 'then'
   engine.showPostsOnOnePage();
});

现在,用户还可以的不可以提供的补充承诺链条:

Now, the user may also not supply a supplement Promise chain link:

var engine = new Engine({path: '/path/to/posts'});

// ERROR
// engine will not be available as an Engine object here

这可能会带来问题,因为用户可能会困惑,为什么引擎不是施工后用。

在构造函数中使用承诺的原因是有道理的。我想整个博客到施工阶段后正常运行。然而,这似乎是一个气味几乎不能访问该对象调用后,立即

The reason to use a Promise in the constructor makes sense. I want the entire blog to be functioning after the construction phase. However, it seems like a smell almost to not have access to the object immediately after calling new.

我一直在使用沿着 engine.start(东西线)的争论。那么() engine.init()这将使该承诺来代替。但是,这些似乎也臭。

I have debated using something along the lines of engine.start().then() or engine.init() which would return the Promise instead. But those also seem smelly.

编辑:这是一个Node.js的项目

This is in a Node.js project.

推荐答案

是的,这是一种不好的做法。构造函数应该返回其类,闲来无事的一个实例。它会弄乱<一个href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/new\"><$c$c>new运营商和继承,否则。

Yes, it is a bad practise. A constructor should return an instance of its class, nothing else. It would mess up the new operator and inheritance otherwise.

此外,构造函数应该只创建和初始化一个新的实例。应建立数据结构和所有实例特定的属性,但是不会执行的任何任务。它应该是没有副作用如果可能的话纯函数,使用具有的所有好处。

Moreover, a constructor should only create and initialize a new instance. It should set up data structures and all instance-specific properties, but not execute any tasks. It should be a pure function without side effects if possible, with all the benefits that has.

如果我想从我的构造函数中执行的东西是什么?

What if I want to execute things from my constructor?

这应该在你的类的方法。你想变异全局状态?然后调用这一程序明确,而不是作为生成对象的副作用。此调用可以实例化后,向右走:

That should go in a method of your class. You want to mutate global state? Then call that procedure explicitly, not as a side effect of generating an object. This call can go right after the instantiation:

var engine = new Engine()
engine.displayPosts();

如果该任务是异步的,你现在可以轻松地返回一个承诺,从方法及其结果,可以轻松地等待,直到它完成。结果
我不过不推荐这种模式时,该方法(异步)变异实例等方法取决于,因为这将导致它们被要求等待(异步成为即使他们实际上是同步)和你迅速拥有一些内部队列管理回事。不要存在未code实例,但实际上是不可用的。

If that task is asynchronous, you can now easily return a promise for its results from the method, to easily wait until it is finished.
I would however not recommend this pattern when the method (asynchronously) mutates the instance and other methods depend on that, as that would lead to them being required to wait (become async even if they're actually synchronous) and you'd quickly have some internal queue management going on. Do not code instances to exist but be actually unusable.

如果我想将数据加载到我的实例异步?

What if I want to load data into my instance asynchronously?

问问自己:你真的需要在没有数据的情况下?你能以某种方式使用它?

如果这个问题的答案是的没有的,那么你的数据之前,您应该不会创建它。 ifself一个参数的数据对您的构造函数,而不是告诉构造如何获取的数据(或者通过为数据的承诺)。

If the answer to that is No, then you should not create it before you have the data. Make the data ifself a parameter to your constructor, instead of telling the constructor how to fetch the data (or passing a promise for the data).

然后,使用一个静态方法来加载数据,从中返回的承诺。然后将它与包装中的数据,新的实例上的呼叫:

Then, use a static method to load the data, from which you return a promise. Then chain a call that wraps the data in a new instance on that:

Engine.load({path: '/path/to/posts'}).then(function(posts) {
    new Engine(posts).displayPosts();
});

这允许获取数据的途径了更大的灵活性,并简化构造了很多。同样的,你可能会写静态工厂函数返回的承诺为引擎实例:

This allows much greater flexibility in the ways to acquire the data, and simplifies the constructor a lot. Similarly, you might write static factory functions that return promises for Engine instances:

Engine.fromPosts = function(options) {
    ajax(options.path).then(Engine.parsePosts).then(function(posts) {
        return new Engine(posts, options);
    });
};

…

Engine.fromPosts({path: '/path/to/posts'}).then(function(engine) {
    engine.registerWith(framework).then(function(framePage) {
        engine.showPostsOn(framePage);
    });
});

这篇关于它是不好的做法有一个构造函数返回一个承诺?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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