You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
functionasyncGreet(name){// perform some asynchronous operation, resolve or reject the promise when appropriate.return$q(function(resolve,reject){setTimeout(function(){if(okToGreet(name)){resolve('Hello, '+name+'!');}else{reject('Greeting '+name+' is not allowed.');}},1000);});}varpromise=asyncGreet('Robin Hood');promise.then(function(greeting){alert('Success: '+greeting);},function(reason){alert('Failed: '+reason);});
functionasyncGreet(name){vardeferred=$q.defer();setTimeout(function(){deferred.notify('About to greet '+name+'.');if(okToGreet(name)){deferred.resolve('Hello, '+name+'!');}else{deferred.reject('Greeting '+name+' is not allowed.');}},1000);returndeferred.promise;}varpromise=asyncGreet('Robin Hood');promise.then(function(greeting){alert('Success: '+greeting);},function(reason){alert('Failed: '+reason);},function(update){alert('Got notification: '+update);});
一. $q的构成
二. 用法
1.像ES6标准的Promise的用法一样的构造器风格:
1. 该用法不支持progress/notify回调用法
2. 在resolver这个构造方法里抛出的异常,并不会自动调用该Promise的reject方法
2. 传统的CommonJS-style用法:
注意 :
三.$q和Kris Kowal's Q的差异
The text was updated successfully, but these errors were encountered: