How would you make an angularjs service return a promise? Write a code snippet as an example



How would you make an angularjs service return a promise? Write a code snippet as an example..

Answer / Bhup Singh

[n function MyService($q) {n this.myMethod = function() {n var deferred = $q.defer();n // Some asynchronous operation heren deferred.resolve('Result');n return deferred.promise;n }n }n]nInject MyService into your controller and call `myService.myMethod().then(function(result) {...})` to handle the promise.

Is This Answer Correct ?    0 Yes 0 No

Post New Answer

More AngularJS Interview Questions

Explain angular js boot process.

1 Answers  


Explain the concept of scope. How does scope inheritance work in angularjs?

1 Answers  


How to set http request header in angular 4 and angular 2?

1 Answers  


Does google use angular?

1 Answers  


What is the use of @Input and @Output?

1 Answers  


Explain component specific hooks?

1 Answers  


How AngularJS compilation is different from other JavaScript frameworks?

1 Answers  


What is an async pipe?

1 Answers  


How to append base url to http requests in angular?

1 Answers  


What is inject in angular?

1 Answers  


How would you pass data from a parent component to a child component?

1 Answers  


How to install angular 8?

2 Answers  


Categories