那么注册的函数可以像Promise Constructor那样获得自己的解析方法吗? [英] Can the function registered by then get its own resolve method just like Promise Constructor?

查看:168
本文介绍了那么注册的函数可以像Promise Constructor那样获得自己的解析方法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

然后方法注册/执行的功能是否能够抓住自己的决心?



一个承诺,当它的构造能够注入其解决方法,如:

 新Promise(功能(解决,拒绝) {async(resolve();})

然而,正常注册的动作通常在resolve方法中触发喜欢(至少在一些我看过的例子中):

  resolve = function(value){
self.value = value;
self.state =resolved;
deferred.resolve(deferred.transform(self.value));
}
/ pre>

没有真正的方法将 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 into deferred.transform method. It almost as if the then 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 own resolve which is the deferred.resolve? The transformation function can be ansychronous and it can resolve 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屋!

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