I tried using Angular with Bluebird promises:
HTML:
<body ng-app="HelloApp"> <div ng-controller="HomeController">{{name}} {{also}}</div> </body>
JS:
// javascript var app = angular.module('HelloApp', []); app.controller("HomeController", function ($scope) { var p = Promise.delay(1000).then(function () { $scope.name = "Bluebird!"; console.log("Here!", $scope.name); }).then(function () { $scope.also = "Promises"; }); $scope.name = "$q"; $scope.also = "promises"; }); window.app = app;
[Fiddle]
However, no matter what I tried, it kept staying "$q promises"
and did not update. Except if I added a manual $scope.$apply
which I'd rather avoid.
(I know it's possible since $q does it)
I'm using Bluebird 2.0 which I got here.
Integration With Async/Await Unfortunately there is no way to get async functions to return Bluebird promises. Even if you set global. Promise = require('bluebird'); , async functions will still return native promises.
There are many ways to use bluebird in browsers: Full build bluebird.js Full build minified bluebird.min.js Core build minified bluebird.core.min.js You may use the bower package. When using script tags the global variables Promise and P (alias for Promise) become available. Bluebird runs on a wide variety of browsers including older versions.
You may use the bower package. When using script tags the global variables Promise and P (alias for Promise) become available. Bluebird runs on a wide variety of browsers including older versions. We'd like to thank BrowserStack for giving us a free account which helps us test that.
The ng-bootstrap repository allows you to use Bootstrap with Angular without any dependency on jQuery or Bootstrap’s JavaScript. It contains native Angular directives based on Bootstrap’s HTML markup and CSS. So, you can have Bootstrap’s styles with the power of Angular with ng-Bootstrap.
Well, if we look at how Angular's own promises work, we need to get Bluebird to $evalAsync
somewhere in order to get the exact same behavior.
If we do that, the fact both implementations are Promises/A+ compliant means we can interop between $q
code and Bluebird code, meaning we can use all of Bluebird's features in Angular code freely.
Bluebird exposes this functionality, with its Promise.setScheduler
functionality:
// after this, all promises will cause digests like $q promises. function trackDigests(app) { app.run(["$rootScope",function ($rootScope) { Promise.setScheduler(function (cb) { $rootScope.$evalAsync(cb); }); }]); }
Now all we have to do is add a:
trackDigests(app);
line after the var app = ...
line, and everything will work as expected. For bonus points, put Bluebird in a service so you can inject it rather than use it on the global namespace.
Here is a [Fiddle] illustrating this behavior.
Note that besides all the features Bluebird has over $q
, one of the more important ones is that Bluebird will not run $exceptionHandler
, but instead will automatically track unhandled rejections, so you can throw
freely with Bluebird promises and Bluebird will figure them out. Moreover calling Promise.longStackTraces()
can help with debugging a lot.
Library Angular bluebird promises replaces $q
service with bluebird
. $http
also run through bluebird
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With