Skip to content
This repository was archived by the owner on Feb 26, 2024. It is now read-only.
This repository was archived by the owner on Feb 26, 2024. It is now read-only.

Patched bluebird does not provide data to promise chain #1112

Closed
@jrabbe

Description

@jrabbe

Just started investigating the upgrade of a large application based on AngularJS to Angular. This application relies on Bluebird as its global promise object and uses webpack as its module loader. The prescribed solutions mentioned in #455 were unsuccessful, as the promises from webpack kept resolving to undefined instead of the relevant view and controller content.

...
import './polyfills';

import * as Bluebird from 'bluebird';

import 'zone.js';
import 'zone.js/dist/zone-bluebird';

... 

// Patch bluebird to be zone aware. This also sets the global Promise object
// to be bluebird.
// Hackishly cast Zone so we can index it by the symbol for "bluebird" as
// patched by zone-bluebird.
(Zone as any)[Zone.__symbol__('bluebird')](Bluebird);

// Disable promise checking in zone.js because it doesn't recognize Bluebird as 
// zone aware, even when patched.
Zone.assertZonePatched = () => {};

...

After investigation the reason for these failures seem to be the way the promises are resolved in the bluebird patch function. The callbacks for .then, .spread, and .finally are patched to schedule the invocation as a zone.js micro task. However, the result from the callbacks is never made available to bluebird breaking the promise chain. Specifically, the call to zone.scheduleMicroTask returns a task, but that task is never saved, and the result of the task invocation is not passed back to Bluebird.

To solve this I have patched the Bluebird patch to return promises from the callbacks, ensuring Bluebird waits for the values to resolve when the micro task is invoked, and allowing the values to continue into the promise chain.

diff --git a/web/webpack/node_modules/zone.js/lib/extra/bluebird.ts b/web/webpack/node_modules/zone.js/lib/extra/bluebird.ts
index 13a0496eaa..0e8ef3b358 100644
--- a/web/webpack/node_modules/zone.js/lib/extra/bluebird.ts
+++ b/web/webpack/node_modules/zone.js/lib/extra/bluebird.ts
@@ -22,11 +22,21 @@ Zone.__load_patch('bluebird', (global: any, Zone: ZoneType, api: _ZonePrivate) =
           const func = args[i];
           if (typeof func === 'function') {
             args[i] = function() {
+              let callbackResolve, callbackReject;
+              const promise = new Bluebird((resolve, reject) => {
+                callbackResolve = resolve;
+                callbackReject = reject;
+              });
               const argSelf: any = this;
               const argArgs: any = arguments;
               zone.scheduleMicroTask('Promise.then', () => {
-                return func.apply(argSelf, argArgs);
+                try {
+                  callbackResolve(func.apply(argSelf, argArgs));
+                } catch (e) {
+                  callbackReject(e);
+                }
               });
+              return promise;
             };
           }
         }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions