1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80
   81
   82
   83
   84
   85
   86
   87

ash / webui / common / resources / promise_resolver.js [blame]

// Copyright 2016 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// This file is deprecated in favor of
// ui/webui/resources/js/promise_resolver.ts, and is maintained only for
// legacy Closure Compiler users.

import {assertNotReached} from 'chrome://resources/ash/common/assert.js';

/**
 * @fileoverview PromiseResolver is a helper class that allows creating a
 * Promise that will be fulfilled (resolved or rejected) some time later.
 *
 * Example:
 *  var resolver = new PromiseResolver();
 *  resolver.promise.then(function(result) {
 *    console.log('resolved with', result);
 *  });
 *  ...
 *  ...
 *  resolver.resolve({hello: 'world'});
 */

/** @template T */
// eslint-disable-next-line no-var
export var PromiseResolver = class {
  constructor() {
    /** @private {function(T=): void} */
    this.resolve_;

    /** @private {function(*=): void} */
    this.reject_;

    /** @private {boolean} */
    this.isFulfilled_ = false;

    /** @private {!Promise<T>} */
    this.promise_ = new Promise((resolve, reject) => {
      this.resolve_ = /** @param {T=} resolution */ (resolution) => {
        resolve(resolution);
        this.isFulfilled_ = true;
      };
      this.reject_ = /** @param {*=} reason */ (reason) => {
        reject(reason);
        this.isFulfilled_ = true;
      };
    });
  }

  /** @return {boolean} Whether this resolver has been resolved or rejected. */
  get isFulfilled() {
    return this.isFulfilled_;
  }

  set isFulfilled(i) {
    assertNotReached();
  }

  /** @return {!Promise<T>} */
  get promise() {
    return this.promise_;
  }

  set promise(p) {
    assertNotReached();
  }

  /** @return {function(T=): void} */
  get resolve() {
    return this.resolve_;
  }

  set resolve(r) {
    assertNotReached();
  }

  /** @return {function(*=): void} */
  get reject() {
    return this.reject_;
  }

  set reject(s) {
    assertNotReached();
  }
};