I am trying to understand why the following code behaves differently with Q.defer () and Promise ()
Case 1 : when I use 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');
Exit:
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'); }
Exit:
in first then from two
Question
- Why is there a difference in output?
- I know that Q.defer is an anti-pattern, but then how to choose when to use what?
source share