Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between Q.defer() & Promise()

I'm trying to understand why following code is behaving differently with Q.defer() and Promise()

Case 1 : When I'm using Q.defer()

getDocument(id)
.then(function (response) {
   console.log('in first then')
   return 'from two';
}).then(function (response) {
   console.log(response)
});

var getDocument=function(){
  var b = Q.defer();
    b.resolve('from getDocument'); // here will do some async operation..this is just an example
  return b.promise;
}

Output:

in first then
undefined

Case 2: using Promise()

getDocument(id)
.then(function (response) {
   console.log('in first then')
   return 'from two';
}).then(function (response) {
   console.log(response)
});

var getDocument=function(){
  return Promise.resolve('from getDocument');
}

Output:

in first then        
from two

Question

  1. Why there is a difference in output?
  2. I know that Q.defer is an anti-pattern but then how to choose when to use what?
like image 347
Rishi Tiwari Avatar asked Jan 09 '17 04:01

Rishi Tiwari


People also ask

What is Q defer ()?

$q. defer() allows you to create a promise object which you might want to return to the function that called your login function.

What does $q do in AngularJS?

$q is an angular defined service. It's the same as new Promise(). But $q takes things to the next level by enhancing additional feature that developers can use to perform complex tasks more simply. resolve(value) – resolves the derived promise with the value.

What is deferred in Nodejs?

Deferred is complete, one of the fastest and natural promise implementation in JavaScript, with Deferred you can write clear maintainable code that takes maximum out of asynchronicity, in fact due to multi-dimensional nature of promises (chaining and nesting) you're forced to program declaratively.

Which of the following properties promise object support?

The Promise object supports two properties: state and result. While a Promise object is "pending" (working), the result is undefined. When a Promise object is "fulfilled", the result is a value. When a Promise object is "rejected", the result is an error object.


1 Answers

In fact, both examples are returning the same result (the same order). Check this codepen Example

var getDocument=function(){
  var b = Q.defer();
    b.resolve('Q from getDocument'); // here will do some async operation..this is just an example
  return b.promise;
}

getDocument(1)
.then(function (response) {
   console.log('Q in first then')
   return 'Q from two';
}).then(function (response) {
   console.log(response)
});

var getDocumentP=function(){
  return Promise.resolve('P from getDocument');
}

getDocumentP(1)
.then(function (response) {
   console.log('P in first then')
   return 'P from two';
}).then(function (response) {
   console.log(response)
});

2) You can see here some uses of Q.defer: Q.defer you´re doing it wrong

like image 139
Tulio Faria Avatar answered Sep 30 '22 17:09

Tulio Faria