showing results for - " 24q use in angularjs"
Alessandra
08 Feb 2017
1function MyService($q) {
2  return {
3    getSomething() {
4      return $q((resolve, reject) => {
5        if (/* some async task is all good */) {
6          resolve('Success!');
7        } else {
8          reject('Oops... something went wrong');
9        }
10      });
11    }
12  };
13}
14
15angular
16  .module('app')
17  .service('MyService', MyService);
18
Vincenzo
10 Aug 2020
1it('should simulate promise', inject(function($q, $rootScope) {
2  var deferred = $q.defer();
3  var promise = deferred.promise;
4  var resolvedValue;
5
6  promise.then(function(value) { resolvedValue = value; });
7  expect(resolvedValue).toBeUndefined();
8
9  // Simulate resolving of promise
10  deferred.resolve(123);
11  // Note that the 'then' function does not get called synchronously.
12  // This is because we want the promise API to always be async, whether or not
13  // it got called synchronously or asynchronously.
14  expect(resolvedValue).toBeUndefined();
15
16  // Propagate promise resolution to 'then' functions using $apply().
17  $rootScope.$apply();
18  expect(resolvedValue).toEqual(123);
19}));
Nora
22 Apr 2016
1const handleThirdPartyCallback = someArgument => {
2  let promise = new Promise((resolve, reject) => {
3    // assuming some third-party API, that is *not* a Promise Object
4    // but fires a callback once finished
5    myCallbackLib(someArgument, response => {
6      // we can resolve it with the response
7      resolve(response);
8    }, reason => {
9      // we can reject it with the reason
10      reject(reason);
11    });
12  });
13  return promise;
14};
15
16handleThirdPartyCallback({ user: 101 }).then(data => {
17  console.log(data);
18});
19