如何编写适合Promise.resolve()的诺言的多票?
我试图写一个诺言多填充,以更好地了解诺言。 我已经搜索了互联网,并找到了一个可以在某种程度上理解的代码。
function CustomPromise(executor) {
var state=PENDING;
var value = null;
var handlers=[];
var catchers = [];
function resolve(result) {
if(state!==PENDING) return;
state=FULFILLED;
value = result;
handlers.forEach((h)=>h(value)); //this line
}
function reject(error) {
if(state!==PENDING)return;
state=REJECTED;
value=error;
catchers.forEach(c=>c(value)); // this line
}
this.then = function(successCallback) {
if(state === FULFILLED) {
successCallback(value);
}else {
handlers.push(successCallback);
}
return this;
}
this.catch = function(failureCallback) {
if(state===REJECTED){
failureCallback(value)
} else {
catchers.push(value);
}
}
executor(resolve,reject);
}
即使在这方面,我也无法理解处理人员和捕手的使用。据说他们是在未实现或拒绝承诺的情况下。解释这两条线也将有所帮助。 现在,上述实现的实际问题是,当使用Let p1 = Promise.resolve(“ Hello World”);
时,它不适用于使用。我尝试将其转换为基于课堂的,但我无法做到这一点。 我的尝试:
class CustomPromise {
constructor(callback){
this.state = PENDING;
this.executor = callback;
this.value = null;
this.handlers = [];
this.catchers = [];
this.then = function(successCallback) {
if(this.state === FULFILLED) {
successCallback(this.value);
}else {
this.handlers.push(successCallback);
}
return this;
};
this.catch = function(failureCallback) {
if(this.state===REJECTED){
failureCallback(this.value)
} else {
this.catchers.push(this.value);
}
};
}
static resolve(result) {
if(this.state!==PENDING) return;
this.state=FULFILLED;
this.value = result;
this.handlers.forEach((h)=>h(this.value));
// return new CustomPromise( function ( fulfil ) {
// fulfil( value );
// });
}
static reject(error) {
if(this.state!==PENDING)return;
this.state=REJECTED;
this.value=error;
this.catchers.forEach(c=>c(this.value));
}
// executor(resolve,reject);
}
有人可以纠正功能方法,以便它适用于ustompromise.resolve()
方案或基于我类的方法中的校正。 编辑:尝试custompromise.prototype.resolve = function(error){...}
仍然有相同的错误custompromise.resolve不是函数
edit2:在基于类的方法中,我无法实现 executor 回调。我只想使用Promise.resolve()
的案例工作方法之一
I'm trying to write a promise polyfill to get a better understanding of promise.
I've searched the internet and found a code which I'm able to understand to some extent.
function CustomPromise(executor) {
var state=PENDING;
var value = null;
var handlers=[];
var catchers = [];
function resolve(result) {
if(state!==PENDING) return;
state=FULFILLED;
value = result;
handlers.forEach((h)=>h(value)); //this line
}
function reject(error) {
if(state!==PENDING)return;
state=REJECTED;
value=error;
catchers.forEach(c=>c(value)); // this line
}
this.then = function(successCallback) {
if(state === FULFILLED) {
successCallback(value);
}else {
handlers.push(successCallback);
}
return this;
}
this.catch = function(failureCallback) {
if(state===REJECTED){
failureCallback(value)
} else {
catchers.push(value);
}
}
executor(resolve,reject);
}
Even in this I'm unable to understand the use of handlers and catchers. It was said that they are for situation when promise is not fulfilled or rejected. Explaining these two lines will also help.
Now, the actual issue with above implementation is it doesn't work for when used like let p1 = Promise.resolve("Hello World");
. I have tried converting it to class based but I'm unable to do that.
My attempt:
class CustomPromise {
constructor(callback){
this.state = PENDING;
this.executor = callback;
this.value = null;
this.handlers = [];
this.catchers = [];
this.then = function(successCallback) {
if(this.state === FULFILLED) {
successCallback(this.value);
}else {
this.handlers.push(successCallback);
}
return this;
};
this.catch = function(failureCallback) {
if(this.state===REJECTED){
failureCallback(this.value)
} else {
this.catchers.push(this.value);
}
};
}
static resolve(result) {
if(this.state!==PENDING) return;
this.state=FULFILLED;
this.value = result;
this.handlers.forEach((h)=>h(this.value));
// return new CustomPromise( function ( fulfil ) {
// fulfil( value );
// });
}
static reject(error) {
if(this.state!==PENDING)return;
this.state=REJECTED;
this.value=error;
this.catchers.forEach(c=>c(this.value));
}
// executor(resolve,reject);
}
Can someone correct the functional approach so that it works for CustomPromise.resolve()
scenario or correction in my class based approach will also be appreciated.
EDIT: Tried CustomPromise.prototype.resolve = function(error) {...}
still getting same error CustomPromise.resolve is not a function
EDIT2 : In class based approach I'm unable to implement executor callback. I just want either one of the approach to work for case like Promise.resolve()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
要扩展
customPromise
的第一个(工作)版本,请在您所拥有的(非工作)类中添加您似乎一直在注释的代码中尝试的代码。但这有两个小问题。尽管如此,使用新的自定义promise
的想法是正确的:因此,如果将其添加到
自定义
函数>函数定义下方,则它将工作。其次,由于
Catchers
应该具有回调函数,因此您不应将value
推向其,而是failurecallback
:免责声明:此polyfill不符合 promises/a+规格让我们符合<<<<<<代码> Promise 。
它遇到的一些问题:
然后
回调同步执行。然后,
回调绝不应同步执行,但始终通过排队的作业(即异步)来调用。catch
方法错误地返回未定义。它应该回报承诺。然后
方法错误地返回了与所谓的相同的承诺。它应该返回一个不同的承诺,以回调将返回的价值解决。然后
方法忽略了第二个参数。它应该采用第二个功能,该功能应用作拒绝回调(例如catch
)。有许多正确的实现。我发布了自己的诺言/A+兼容实施在此答案中。它没有
Resolve
和拒绝
静态方法,但它需要与上面给出的附加代码相同。To extend the first (working) version of
CustomPromise
, add the code you seem to have been trying with in commented-out code in the (non-working) class-version you had. But it had two little problems. Still, the idea to usenew CustomPromise
is the right one:So if you add that below your
CustomPromise
function definition, it'll work.Secondly, as
catchers
is supposed to have callback functions, you should not pushvalue
to it, butfailureCallback
:Disclaimer: this polyfill is not compliant with the Promises/A+ specification let be it would be compliant with the ECMAScript specification for
Promise
.Some of the problems it has:
then
callback to be executed synchronously. Thethen
callback should never be executed synchronously, but always be called via a queued job (i.e. asynchronously).catch
method wrongly returns undefined. It should return a promise.then
method wrongly returns the same promise as it is called on. It should return a different promise, that resolves with the value that will be returned by the callback.then
method ignores a second argument. It should take a second function which should serve as a rejection callback (likecatch
).There are many correct implementations to be found. I posted my own Promises/A+ compliant implementation in this answer. It does not have the
resolve
andreject
static methods, but it would require the same additional code as given above.