$q use in angularjs
function MyService($q) {
return {
getSomething() {
return $q((resolve, reject) => {
if (/* some async task is all good */) {
resolve('Success!');
} else {
reject('Oops... something went wrong');
}
});
}
};
}
angular
.module('app')
.service('MyService', MyService);