TypeScript 等价于 Dart `Completer`

Gün*_*uer 2 asynchronous dart typescript

在 Dart 中,我有一个Completer返回Future(Promise) 并且Future可以在其他地方完成然后创建它的地方,比如

class SomeClass {
  final Completer<bool> initializationDone = new Completer<bool>();

  SomeClass() {
    _doSomeAsyncInitialization();
  }

  void _doSomeAsyncInitialization() {
    // some async initialization like a HTTP request
    fetchDataFromServer().then((data) {
      processData();
      initializationDone.complete(true);
    });
  }
}

main() {
  var some = new SomeClass();
  some.initializationDone.future.then((success) {
    // do something.
  });
}
Run Code Online (Sandbox Code Playgroud)

我不想要这个实际问题的解决方案,这只是我想出的一个例子来演示如何Completer在 Dart 中使用a 。什么相当于CompleterTypeScript 中的 Dart ?

Jon*_*han 5

制作了这个对我有用的简单课程:

export class Completer<T> {
    public readonly promise: Promise<T>;
    public complete: (value: (PromiseLike<T> | T)) => void;
    private reject: (reason?: any) => void;

    public constructor() {
        this.promise = new Promise<T>((resolve, reject) => {
            this.complete = resolve;
            this.reject = reject;
        })
    }
}

const prom = new Completer<bool>();

prom.complete(true);
Run Code Online (Sandbox Code Playgroud)