【问题标题】:a promise was created in a handler at ... but was not returned from it在 ... 的处理程序中创建了一个承诺,但没有从它返回
【发布时间】:2017-08-11 05:41:50
【问题描述】:

我正在尝试解决 BsDropDownState 中的此 ngx-bootstrap 警告
我正在使用 angular 4.3.3 和 bluebird 3.5.0

Bluebird 产生以下警告。

警告:在 ... 的处理程序中创建了一个承诺,但没有从中返回,请参阅 google docs

我观察到的是,以这种方式创建的任何承诺都会产生相同的警告,无论它是否被调用?

var BsDropdownState = (function () {
    function BsDropdownState() {
        var _this = this;
        this.direction = 'down';

        /* this empty promise that is never called produces the same warning */
        this.test = new Promise(function (resolve) { resolve("TEST"); return null; })
     }
}

这里的正确模式是什么?

完整的原始打字稿如下

import { EventEmitter, Injectable } from '@angular/core';
import { BsComponentRef } from '../component-loader/bs-component-ref.class';

@Injectable()
export class BsDropdownState {
  direction: 'down' | 'up' = 'down';
  autoClose: boolean;
  isOpenChange = new EventEmitter<boolean>();
  isDisabledChange = new EventEmitter<boolean>();
  toggleClick = new EventEmitter<boolean>();

  /**
   * Content to be displayed as popover.
   */
  dropdownMenu: Promise<BsComponentRef<any>>;
  resolveDropdownMenu: (componentRef: BsComponentRef<any>) => void;

  constructor() {
    // here lies the sleeping warning...
    this.dropdownMenu = new Promise((resolve) => {
      this.resolveDropdownMenu = resolve;
    });
  }
}

编辑

我尝试将返回值添加到生成的.js。警告仍然存在

var BsDropdownState = (function () {
    function BsDropdownState() {
        var _this = this;
        this.direction = 'down';
        this.isOpenChange = new __WEBPACK_IMPORTED_MODULE_0__angular_core__["EventEmitter"]();
        this.isDisabledChange = new __WEBPACK_IMPORTED_MODULE_0__angular_core__["EventEmitter"]();
        this.toggleClick = new __WEBPACK_IMPORTED_MODULE_0__angular_core__["EventEmitter"]();
        this.dropdownMenu = new Promise(function (resolve) {
            _this.resolveDropdownMenu = resolve;
            resolve("TESTING");
            return null;
        });
        return null;
    }

编辑

这里是(部分)stack trace
core.es5.js

【问题讨论】:

  • 正确,因为function BsDropdownState() { 不返回任何内容
  • 它是一个构造函数吗?我想这就是让我感到困惑的原因,正确的模式是什么?
  • 你是直接在某个地方调用这个构造函数,还是被某个自动进程调用?如果是前者,你能告诉我们它被调用的代码吗?
  • @JLRishe,我正在通过 core.es5.js 添加堆栈跟踪
  • @Jim 不确定您的用例是什么,但通常的模式是创建一个 promise,使用 .then 来利用来自 promise 的值 resolved,或者 .catch any错误。并且可以选择等待承诺。您的代码中出现的一些问题是: 1. 如果您不需要传递它,那么存储 Promise 的需要是什么? 2、this.resolveDropdownMenu = resolve;有什么用?同样,我不确定您的用例,只是尝试根据给定的代码提供帮助。

标签: javascript typescript bluebird es6-promise


【解决方案1】:

您需要使用resolvereject 来返回一个已履行和被拒绝的承诺。一般架构如下:

const myFirstPromise = new Promise((resolve, reject) => {
  // do something asynchronous which eventually calls either:
  //
  //   resolve(someValue); // fulfilled
  // or
  //   reject("failure reason"); // rejected
});

查看the source了解更多详情。

希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 2016-03-01
    • 1970-01-01
    • 2016-10-18
    • 1970-01-01
    • 1970-01-01
    • 2017-11-02
    • 1970-01-01
    • 2016-09-20
    • 2016-08-23
    相关资源
    最近更新 更多