@@ -221,6 +221,52 @@ Zone.bindArgumentsOnce = function (args) {
221221 return args ;
222222} ;
223223
224+ /*
225+ * patch a fn that returns a promise
226+ */
227+ Zone . bindPromiseFn = ( function ( ) {
228+ // if the browser natively supports Promises, we can just return a native promise
229+ if ( window . Promise ) {
230+ return function ( delegate ) {
231+ return function ( ) {
232+ var delegatePromise = delegate . apply ( this , arguments ) ;
233+ if ( delegatePromise instanceof Promise ) {
234+ return delegatePromise ;
235+ } else {
236+ return new Promise ( function ( resolve , reject ) {
237+ delegatePromise . then ( resolve , reject ) ;
238+ } ) ;
239+ }
240+ } ;
241+ } ;
242+ } else {
243+ // if the browser does not have native promises, we have to patch each promise instance
244+ return function ( delegate ) {
245+ return function ( ) {
246+ return patchThenable ( delegate . apply ( this , arguments ) ) ;
247+ } ;
248+ } ;
249+
250+ function patchThenable ( thenable ) {
251+ var then = thenable . then ;
252+ thenable . then = function ( ) {
253+ var args = Zone . bindArguments ( arguments ) ;
254+ var nextThenable = then . apply ( thenable , args ) ;
255+ return patchThenable ( nextThenable ) ;
256+ } ;
257+
258+ var ocatch = thenable . catch ;
259+ thenable . catch = function ( ) {
260+ var args = Zone . bindArguments ( arguments ) ;
261+ var nextThenable = ocatch . apply ( thenable , args ) ;
262+ return patchThenable ( nextThenable ) ;
263+ } ;
264+ return thenable ;
265+ }
266+ }
267+ } ( ) ) ;
268+
269+
224270Zone . patchableFn = function ( obj , fnNames ) {
225271 fnNames . forEach ( function ( name ) {
226272 var delegate = obj [ name ] ;
0 commit comments