Search code examples
promisebluebird

Promise.try with args alternatives


In Bluebird 3.0 changelog it now says that Promise.try with ctx and arts parameters is now deprecated, but, contrary to the other deprecation warnings, it doesn't suggest any method to migrate those calls.

What is the best way to migrate to bluebird 3 any such calls? Can the reason for the deprecation be read somewhere?

Thanks


Solution

  • I've been not able to find the reference on bluebird 2.0 API, but if I understand things in the right way from the context, you can use a closure to achieve the effect like so:

    bluebird 2.0:

    Promise.try(obj.method, obj, arg)
    Promise.try(globalFunction, this, arg)
    

    bluebird 3.0:

    Promise.try(() => obj.method( arg ))
    Promise.try(() => globalFunction.call(this, arg))
    

    or, if you are not able to use ES2015 syntax:

    Promise.try(function () { return obj.method(arg) })
    Promise.try(function () { return globalFunction.call(this, arg) }.bind(this))