本文介绍了为什么无极构造需要一个执行者?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用,为什么不能T触发器决心拒绝将在codeBase的其他地方定义的?

When using Promises, why can't triggers for resolve and reject be defined elsewhere in the codebase?

我不明白为什么解析拒绝,其中的承诺声明的逻辑应该本地化。这是一个疏忽,还是有强制要在执行人的利益参数?

I don't understand why resolve and reject logic should be localized where the promise is declared. Is this an oversight, or is there a benefit to mandating the executor parameter?

我相信执行程序功能应该是可选的,它的存在应确定是否承诺解决封装与否。该承诺将更加扩展没有这样的任务,因为你没有启动异步的时候了。承诺也应复位。这是一个1次射门开关,1或0,解析()拒绝()。有并行和顺序成果众多,可连接: promise.then(parallel1) promise.then(parallel2) promise.then(SEQ1)。然后(SEQ2),但引用特权球员无法解析/拒绝进入交换机

I believe the executor function should be optional, and that its existence should determine whether the promise encapsulates resolution or not. The promise would be much more extensible without such mandates, since you don't have to initiate async right away. The promise should also be resettable. It's a 1 shot switch, 1 or 0, resolve() or reject(). There are a multitude of parallel and sequential outcomes that can be attached: promise.then(parallel1) and promise.then(parallel2) and also promise.then(seq1).then(seq2) but reference-privileged players cannot resolve/reject INTO the switch

您可以在以后的时间构建结果树,但你不能改变它们,也无法改变的根源(输入触发)

老实说,连续结果的树应该是edittable以及..说你要到别的拼接出一步,做一些事情,而不是,你已经宣布很多诺言链后。它没有意义重建的承诺和每一个顺序的功能,特别是因为你甚至不能拒绝或破坏承诺要么...

Honestly, the tree of sequential outcomes should be edittable as well.. say you want to splice out one step and do something else instead, after you've declared many promise chains. It doesn't make sense to reconstruct the promise and every sequential function, especially since you can't even reject or destroy the promise either...

推荐答案

这就是所谓通过Domenic创造的。

This is called the revealing constructor pattern coined by Domenic.

基本上,这个想法是给你访问的部分的对象,而该对象没有完全构造呢。引用Domenic:

Basically, the idea is to give you access to parts of an object while that object is not fully constructed yet. Quoting Domenic:

我把这个揭示构造模式,因为无极构造揭示其内部的功能,但仅限于code的构造有问题的承诺。解决或拒绝承诺的能力只透露给了施工code和是至关重要没有透露使用该承诺的人。因此,如果我们p交给另一个消费者,说

首先,承诺与递延对象的工作,在扭曲的承诺JavaScript的承诺起源于这是真的。这仍然是像角的 $ Q ,Q,jQuery和旧版本的蓝鸟。

The past

Initially, promises worked with deferred objects, this is true in the Twisted promises JavaScript promises originated in. This is still true (but often deprecated) in older implementations like Angular's $q, Q, jQuery and old versions of bluebird.

的API去是这样的:

var d = Deferred();
d.resolve();
d.reject();
d.promise; // the actual promise

它的工作,但它有一个问题。 Deferreds和承诺构造通常用于将非承诺的API承诺。有一个JavaScript著名的问题,被称为 - 基本,这意味着一个API必须是同步或异步但从来都立刻

It worked, but it had a problem. Deferreds and the promise constructor are typically used for converting non-promise APIs to promises. There is a "famous" problem in JavaScript called Zalgo - basically, it means that an API must be synchronous or asynchronous but never both at once.

的事情是 - 与deferreds它可能做这样的事情:

The thing is - with deferreds it's possible to do something like:

function request(param) {
   var d = Deferred();
   var options = JSON.parse(param);
   d.ajax(function(err, value) {
      if(err) d.reject(err);
      else d.resolve(value);
   });
}

有一个隐藏的细微错误在这里 - 如果参数是不是一个有效的JSON此功能的引发的同步,这意味着我必须包装每承诺在既有}赶上(E){ .catch(E =&GT返回函数; 来捕获所有错误。

There is a hidden subtle bug here - if param is not a valid JSON this function throws synchronously, meaning that I have to wrap every promise returning function in both a } catch (e) { and a .catch(e => to catch all errors.

诺言构造函数捕获这些异常并将它们转换为的拒绝,这意味着你永远不必担心同步异常VS异步那些与承诺。 (它通过始终在未来勾选执行然后回调守卫你的另一面)。

The promise constructor catches such exceptions and converts them to rejections which means you never have to worry about synchronous exceptions vs asynchronous ones with promises. (It guards you on the other side by always executing then callbacks "in the next tick").

此外,还需要一个额外的类型每个开发者都了解那里的承诺,构造函数不这是pretty不错。

In addition, it also required an extra type every developer has to learn about where the promise constructor does not which is pretty nice.

这篇关于为什么无极构造需要一个执行者?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 09:56