then() 在使用 bluebird promise 时没有被调用

then() not getting called while using bluebird promise

我正在尝试使用 bluebird 库创建一个新的 Promise。其代码如下:

var promise = new Promise(function(resolve, reject) {
    console.log('Promise created');
})
var myPromise = promise.then(function() {
    console.log('Then called');
});

setTimeout(function () {
    console.log('promise successful');
    console.log(myPromise);
}, 3);

但是控件永远不会进入 then 块。我还看到 promise 对象没有任何实现处理程序:

Promise {
  _bitField: 0,
  _fulfillmentHandler0: undefined,
  _rejectionHandler0: undefined,
  _promise0: undefined,
  _receiver0: undefined }

我如何创建一个承诺并link一个then块到相同的。

您需要在Promise函数中调用resolve()

为了能够使用您创建的 Promise,您需要通过分别调用每个函数从您的 Promise 中解决 and/or 拒绝。

var promise = new Promise(function(resolve, reject)) {      
    if (someWork) resolve(1);
    if (!someWork) reject(2);
}

promise.then(function(data) {
  console.log(data); // 1
});