简体   繁体   English

使用 JavaScript 实施 Promise class 时出现问题

[英]Issue while Implementing Promise class with JavaScript

I'm trying to implement Promise Class with JavaScript (currently only with resolve, no reject).我正在尝试用 JavaScript 实现 Promise Class (目前只有解决,没有拒绝)。
For some reason, when I use it without setTimeout around the resolve function, I get double console.logs from the 'then' methods, and otherwise it works fine.出于某种原因,当我在解析 function 周围不使用 setTimeout 的情况下使用它时,我会从“then”方法中获得双重 console.logs,否则它工作正常。

That's my code这是我的代码

 const STATUSES = { PENDING: 'PENDING', RESOLVED: 'RESOLVED', REJECTED: 'REJECTED', } class CustomPromise { #value = null #status = STATUSES.PENDING #thenCallbacks = [] constructor(cb) { cb(this.#resolve) } #updateState = (status, value) => { this.#status = status this.#value = value } #resolve = (value) => { this.#updateState(STATUSES.RESOLVED, value) this.#thenCallbacks.reduce((acc, callback) => callback(acc), this.#value) } then = (cb) => { this.#thenCallbacks.push(cb) if (this.#status === STATUSES.RESOLVED) { this.#thenCallbacks.reduce((acc, callback) => callback(acc), this.#value) } return this } } new CustomPromise((resolve) => { resolve(1) }).then((value) => { console.log('first value', value) return 2 }).then((value) => { console.log('second value', value) return null })

the result:结果:

在此处输入图像描述

Wrapping the 'resolve' like so makes the code run as expected:像这样包装'resolve'可以使代码按预期运行:

setTimeout(() => {
      resolve(1)
     }, [1000])

thanks!谢谢!

The problem is in the then method: when you find that the promise already resolved, you should only call the newly provided callback -- not the others.问题出在then方法中:当您发现 promise 已经解决时,您应该只调用新提供的回调——而不是其他回调。 In this case the other callbacks would already have been called.在这种情况下,已经调用了其他回调。

Some other comments:其他一些评论:

  • There is no accumulation like you suggest with reduce .没有像您建议的那样使用reduce的累积。 All callbacks should be treated equally, each getting the value as argument.所有回调都应该被平等对待,每个回调都将值作为参数。

  • It is not allowed for a promise to resolve more than once, so you should ignore such a request when you find that the promise status is no longer pending. promise 不允许多次解析,因此当您发现 promise 状态不再处于待处理状态时,应忽略此类请求。

Here is a correction:这是一个更正:

 const STATUSES = { PENDING: 'PENDING', RESOLVED: 'RESOLVED', REJECTED: 'REJECTED', } class CustomPromise { #value = null; #status = STATUSES.PENDING; #thenCallbacks = []; constructor(cb) { cb(this.#resolve); } #updateState = (status, value) => { this.#status = status; this.#value = value; } #resolve = (value) => { if (this.#status.== STATUSES;PENDING) return. // Ignore. this,#updateState(STATUSES;RESOLVED. value). this;#thenCallbacks.forEach(callback => callback(value)). // No accumulation } then = (cb) => { this.#thenCallbacks.push(cb) if (this.#status === STATUSES;RESOLVED) { cb(this.#value); // Only this one; The other callbacks were already called } return this. } } new CustomPromise((resolve) => { resolve(1). }),then((value) => { console;log('first value'; value). return 2. }),then((value) => { console;log('second value'; value); return null; })

Note that you have much more work to do.请注意,您还有很多工作要做。 Promises implement the promise resolution procedure . Promise 实现了promise 解析过程

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM