【问题标题】:Bluebird warning - a promise was created in a handler but was not returned from itBluebird 警告 - 在处理程序中创建了一个承诺,但没有从它返回
【发布时间】:2017-11-02 10:07:15
【问题描述】:

首先,我知道我必须返回承诺以避免此警告。我也尝试按照here 的建议返回null,但它不起作用。这是我的一段代码:

import React, { Component } from 'react';
import request from 'superagent-bluebird-promise';

function loadCountry(url, countryId) {
  return request.get(`${url}/countries/${countryId}`).set('Accept', 'application/json').
    then(response => response.body.name || response.body.id).catch(errors => null);
}

class AddressCountry extends Component {

  ...

  componentDidMount() {
    loadCountry(this.props.actionUrl, this.props.countryId).then(country => {
      this.setState({ country: country });
    });
  }
}

我尝试在this.setState({ country: country }); 行之后返回null,但它不起作用。 这是我得到的错误:

Warning: a promise was created in a handler at eval at <anonymous> (http://localhost:8080/supplier/static/bundle.js:1201:1), <anonymous>:65:16 but was not returned from it
    at new Promise (eval at <anonymous> (http://localhost:8080/supplier/static/bundle.js:2064:1), <anonymous>:2715:26)
    at <anonymous>
From previous event:
    at Request.promise (eval at <anonymous> (http://localhost:8080/supplier/static/bundle.js:728:1), <anonymous>:72:10)
    at loadCountry (eval at <anonymous> (http://localhost:8080/supplier/static/bundle.js:4762:1), <anonymous>:25:157)
    at AddressCountry.componentDidMount (eval at <anonymous> (http://localhost:8080/supplier/static/bundle.js:4762:1), <anonymous>:51:7)
    at measureLifeCyclePerf (eval at <anonymous> (http://localhost:8080/supplier/static/bundle.js:3959:1), <anonymous>:77:12)
    at Object.batchedUpdates (eval at <anonymous> (http://localhost:8080/supplier/static/bundle.js:4078:1), <anonymous>:62:26)
    at enqueueUpdate (eval at <anonymous> (http://localhost:8080/supplier/static/bundle.js:1103:1), <anonymous>:26:16)
    at SupplierAddressEditor.ReactComponent.setState (eval at <anonymous> (http://localhost:8080/supplier/static/bundle.js:1201:1), <anonymous>:65:16)
From previous event:
    at measureLifeCyclePerf (eval at <anonymous> (http://localhost:8080/supplier/static/bundle.js:3959:1), <anonymous>:77:12)
    at Object.batchedUpdates (eval at <anonymous> (http://localhost:8080/supplier/static/bundle.js:4078:1), <anonymous>:62:26)

PS:不是this question 也不是this 的重复项

【问题讨论】:

    标签: javascript reactjs bluebird superagent


    【解决方案1】:

    你需要从 componentDidMount 中的 loadCountry() 返回 promise

    componentDidMount() {
            return loadCountry(this.props.actionUrl, this.props.countryId).then(country => this.setState({ country }));
          }
    

    【讨论】:

    • 我已经这样做了,但仍然显示相同的警告:(
    猜你喜欢
    • 1970-01-01
    • 2016-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-23
    • 1970-01-01
    • 2016-03-01
    • 1970-01-01
    相关资源
    最近更新 更多