那么注册的函数可以像Promise Constructor那样获得自己的解析方法吗? [英] Can the function registered by then get its own resolve method just like Promise Constructor?
问题描述
由然后
方法注册/执行的功能是否能够抓住自己的决心?
一个承诺,当它的构造能够注入其解决方法,如:
新Promise(功能(解决,拒绝) {async(resolve();})
然而,正常注册的动作通常在resolve方法中触发喜欢(至少在一些我看过的例子中):
resolve = function(value){
/ pre>
self.value = value;
self.state =resolved;
deferred.resolve(deferred.transform(self.value));
}
没有真正的方法将
deferred.resolve
注入到deferred.transform
方法,几乎就好像然后
不会像Promise构造函数那样期望它将执行为异步的函数,因此它不是' t同构?
在 Javascript Promise Sequence 您实际上创建了8个Promises,这个应用最好在4个Promises中完成。如果由
然后
注册的转换函数可以获得自己的解析
,那么这不能解决, code> deferred.resolve ?转换函数可以是同步的,当它完成后,它可以解析
。解决方案
该方法注册/执行的功能是否能够抓住自己的决心?
No。
或者至少没有返回一个拦截其回调的ifable,但这并不真实。
几乎就好像当时不会执行异步执行的功能
哦,是的。你的误会似乎是基于异步函数是采取回调的功能。他们不是异步函数是那些返回承诺的函数!而
然后
是完全有能力处理他们的。
...不同于Promise Constructor
这是另一回事。
Promise
构造函数在这里是奇怪的。其仅目的是将老式回调函数转换为承诺。只有这些天,您才会处理完全承诺的功能,新API将立即退回承诺。Would the function that is registered/executed by the
then
method be able to catch hold of its own resolve?A promise when its constructed is able to inject its resolve method like:
new Promise(function(resolve, reject){ async(resolve();})
However the action registered by then normally is triggered in resolve method like (at least in some examples I have seen):
resolve = function(value){ self.value = value; self.state = "resolved"; deferred.resolve(deferred.transform(self.value)); }
There is no real means to inject
deferred.resolve
intodeferred.transform
method. It almost as if thethen
doesn't expect the function it would execute to be asynchronous unlike like a Promise Constructor and hence it isn't isomorphic?In Javascript Promise Sequence you actually create 8 Promises for a job that should ideally be done in 4 Promises. Can't this be solved if the transformation function registered by
then
can get hold of its ownresolve
which is thedeferred.resolve
? The transformation function can be ansychronous and it canresolve
it when it's done.解决方案Would the function that is registered/executed by the then method be able to catch hold of its own resolve?
No.
Or at least not without returning a thenable that intercepts its callback, but that doesn't really make sense.
It almost as if the then doesn't expect the function it would execute to be asynchronous
Oh, it does. Your misunderstanding seems to be based on thinking that asynchronous functions are those that take callbacks. They aren't. Asynchronous functions are those that return promises! And
then
is totally capable dealing with them.…unlike the Promise Constructor
It's the other way round. The
Promise
constructor is the odd one out here. Its only purpose is to transform old-style callback-taking functions to promises. You're expected to deal with fully promisified functions only these days, and new APIs will return promises right away.这篇关于那么注册的函数可以像Promise Constructor那样获得自己的解析方法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!