请稍等 ...
×

采纳答案成功!

向帮助你的同学说点啥吧!感谢那些助人为乐的人

使用async...await后,IDE提示有问题

标识符“Promise”重复。编译器在包含异步函数的模块的顶层范围中保留名称“Promise”。ts(2529)图片描述

import { ResolveType, RejectType, Executor } from './actionType'
function isObject(val: any): val is Record<any, any> {
  return val !== null && typeof val === 'object'
}
function isFunction(data: any): data is Function {
  return typeof data === 'function'
}
function isPromise(val: any): val is Promise {
  return isObject(val) && isFunction(val.then)
}
class Promise<T = any> {
  public reject: RejectType;
  public resolve: ResolveType;
  public status: string;
  public resolve_value: any;
  public reject_value: any;
  // 保存成功状态要执行的函数
  public resolve_then_callbacks: (() => void)[] = [];
  // 保存失败状态要执行的函数
  public reject_then_callbacks: (() => void)[] = [];
  constructor(executor: Executor) {
    this.status = "pending";
    this.resolve = (value: any): any => {
      if (this.status === "pending") {
        this.status = "success";
        this.resolve_value = value;
        this.resolve_then_callbacks.forEach(callback => callback())
      }

    }
    this.reject = (value: any): any => {
      if (this.status === "pending") {
        this.status = "full";
        this.reject_value = value;
      }
    }
    try {
      executor(this.resolve, this.reject);
    } catch (err) {
      this.status = "pending";
      this.reject(err.toString());
      throw new Error('程序终止');
    }

  }
  then(resolveInthen: ResolveType, rejectInthen: RejectType) {
    console.log(`进入到then函数`);
    return new Promise((resolve, reject) => {
      let result: any;
      if (this.status === "success") {
        result = resolveInthen(this.resolve_value)
        resolve(result)
      }
      if (this.status === "full") {
        result = rejectInthen(this.reject_value)
        reject(result)
      }
      if (this.status === "pending") {
        this.processManyAsyncAndSync(resolveInthen, rejectInthen, resolve, reject)
      }
    })
  }
  /**
   * 执行多个异步+多级then的处理方法
   * @param resolveInthen 
   * @param rejectInthen 
   * @param resolve 
   * @param reject 
   */
  processManyAsyncAndSync(resolveInthen: ResolveType, rejectInthen: RejectType, resolve: RejectType, reject: RejectType) {
    let result: any;
    this.resolve_then_callbacks.push(async () => {
      result = await resolveInthen(this.resolve_value);
      if (isPromise(result)) {
        resolve(result.resolve_value)
      } else {
        resolve(result)
      }
      console.log(`then中resolve函数参数执行结果`);
    })
  }
}
export { Promise }  

正在回答 回答被采纳积分+3

1回答

keviny79 2022-01-19 16:06:09

同学,这个错误和 async...awiat 没有关系,这是 TS 检测到的 我们自己定义的 Promise名称和 底层Promise名称重名导致的,修改为MyPromise或者其他有意义的名称就可以了

2 回复 有任何疑惑可以回复我~
  • 老师你好,问题是咱们写的是独立模块化的,他为啥加上async和await就有问题呢,运行没什么问题,就是ide出现错误
    回复 有任何疑惑可以回复我~ 2022-01-19 16:18:33
  • 因为 执行 包含 async .... await 的函数 返回的是底层Promise,TS 编译器同时检查到这个Promise和自己本模块中的Promise 重名导致,这一点在我看来 得是 TS 公司开发编译器时忽略的一个bug,应该过滤掉才对。
    回复 有任何疑惑可以回复我~ 2022-01-21 07:18:40
问题已解决,确定采纳
还有疑问,暂不采纳
意见反馈 帮助中心 APP下载
官方微信