简体   繁体   中英

Bluebird Promise: error is not a function

For some reason that I can't find out, this simple piece of code doesn't work.

new Promise(function (resolve, reject) {
  resolve();
}).then(function() {
  console.log("then: ")
}).error(function(err) {
  console.log("err: ", err)
})

It gives me

Uncaught TypeError: (intermediate value).then(...).error is not a function

If I replace error with catch , it runs fine. I would prefer not to catch though.

What am I missing here?

Making this comment an answer since it turned out to be the solution...

If .error() does not exist, then you probably aren't using a Bluebird promise since .error() is not part of the Promise standard so you may just have a built-in promise instead of a Bluebird promise.

Check to make sure that Bluebird is properly included in your project.

This can also happen if you include another package that uses a different promise library.

In my case, I have a Node app that requires Swagger. Swagger uses the Q promise library.

Bluebird and Q are partially compatible. The problem didn't show up until I started using parts of the Bluebird API that Q doesn't have. In particular, Bluebird has "return", whereas Q does not. (Try searching for "bluebird return is not a function" if you want to waste some time.)

There's a project "bluebird-q" designed to allow Bluebird to replace Q.

With Swagger in particular, there's a flag to make Swagger use promises. It might work to promisifyAll Swagger instead of using the usePromise configuration.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM