0

I don't understand below part

var q = require("q"),
BlogPost = require("../models/blogPost");

module.exports = {
    getAllPosts: getAllPosts
};

function getAllPosts() {
    var deferred = q.defer();

    BlogPost
        .find({})
        .sort("-date")
        .exec(function(error, posts) {
            if (error) {
                deferred.reject(error);
            } else {
                deferred.resolve(posts);
            }
        });

    return deferred.promise;
}

I found above code in controller, but can't understand it. Why at the end we use return deferred.promise? How would I use the getAllPosts later? Can't we just return the posts objects?

Ning Sia
  • 139
  • 1
  • 8
  • http://www.html5rocks.com/en/tutorials/es6/promises/ – ceejayoz Sep 05 '16 at 02:07
  • @ceejayoz is promises a part of javascript? why we can't just return the posts object? – Ning Sia Sep 05 '16 at 02:08
  • Yes, Promises are part of JavaScript. You can't return the posts object probably because whatever the `BlogPost` service is doing is asynchronous. – ceejayoz Sep 05 '16 at 02:18
  • 1
    Here you can read the Q documentation on `defer()` https://github.com/kriskowal/q#using-deferreds – Daniel Zendejas Sep 05 '16 at 02:23
  • @ceejayoz what if I do `BlogPost.find({}).sort("-date").exec(callback);` and call the `BlogPost(function(err,posts){ //get my posts here })` – Ning Sia Sep 05 '16 at 02:27
  • @NingSia We have no idea, you haven't shared what `BlogPost`'s code is. The way it's implemented is likely the sensible way, and you just need to understand Promises and asynchronous actions. – ceejayoz Sep 05 '16 at 02:29
  • @ceejayoz https://github.com/aspnetde/thomasbandt.com/blob/master/data/models/blogPost.js – Ning Sia Sep 05 '16 at 02:30
  • Probably should read: [How do I return the value from an asynchronous callback](http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call). – jfriend00 Sep 05 '16 at 03:38

2 Answers2

1

You would consume a function that returns a promise like so:

var getAllPosts = require('./your-module-name.js').getAllPosts;

getAllPosts()
 .then(function(posts) {
  //successfully process the posts
 })
 .catch(function(err) {
  //handle the error
 })
 .finally(function(){
  //clean up anything you need to...
 })
bluetoft
  • 5,373
  • 2
  • 23
  • 26
  • can I use multiple then? is the order matter? can I put `catch` above `then`? – Ning Sia Sep 05 '16 at 02:38
  • 1
    You can put the catch above the then if you'd like. You can do catch.then.catch.then as many times as you want... just return what you need to from each section in the promise chain. – bluetoft Sep 05 '16 at 02:51
0

Promise is the just representation of the asynchronous result.

It has three states:

1-> success

-> Error

-> Pending

deferred is a just object of promise and it returns after processing one of above state.

We can use javascript code without promise but sometimes we have to give it to make our code execute as asynchronous way.

That's why we use promise

Community
  • 1
  • 1
abdulbarik
  • 6,101
  • 5
  • 38
  • 59