9952 explained code solutions for 126 technologies

angularjsHow can I use the $q.defer method in AngularJS?

The $q.defer method in AngularJS is a service that helps you to create and use promises. It allows you to make asynchronous calls and return a promise object that can be used to handle the result of the call.

The $q.defer method takes a single parameter, which is a function that will be called when the promise is resolved. This function should return a promise object.

Here is an example of how to use the $q.defer method in AngularJS:

var deferred = $q.defer();

// do something asynchronous
setTimeout(function() {
  deferred.resolve('Promise resolved!');
}, 1000);

deferred.promise.then(function(result) {
  console.log(result); // 'Promise resolved!'

The code above creates a deferred object and then resolves it after 1 second. Once the deferred object is resolved, the promise object is returned and the then function is called with the result of the promise.

Code explanation

  1. Create a deferred object using $q.defer()
  2. Do something asynchronous
  3. Resolve the deferred object using deferred.resolve()
  4. Retrieve the result of the promise using deferred.promise.then()

Helpful links

Edit this code on GitHub