How to return response to client in the middle of Promise?

Sato

For example,

Comments.findOne({user: req.user.id}).exce()
.then(function(comment) {
  if(comment) {
    // how to make this return immediately and break the rest then?
    return res.json({error: 'Already commented'});
  } else {
    return Posts.findOne({postId: req.params.id}).exec();
  }
})
.then(function(post) {
  if(post) {
    var comment = new Comment({user: req.user.id, data: req.body.comment})
    return {post: post, comment: comment.save()};
  } else {
    return res.json({error: 'Post not exist'});
  }
})
.then(function(data) {
  post.comments.push(comment._id);
  return post.save();
});
.then(function(post) {
  return res.json({ok: 1})
})
.catch(function(e)) {
  return res.json(error: e);
});

Is this promise written right? How to write this kind of promise? Callbacks/Promises is a headache...

jfriend00

You just need to throw or return a reject promise to trigger the error handling in promises as show in this example:

Comments.findOne({user: req.user.id}).exce()
.then(function(comment) {
  if(comment) {
    // to bypass all the other .then() resolve handlers, either
    // throw an error here or return a rejected promise
    throw new Error(res.json({error: 'Already commented'}));
  } else {
    return Posts.findOne({postId: req.params.id}).exec();
  }
})

Promises are "throw safe" which means that .then() will catch any exception thrown and turn it into a rejected promise. This will bypass any following .then() resolve handlers and will instead go to the next reject handler or .catch() handler.


FYI, you should be careful in your code because when you .catch() and then just return from that, it changes your promise state from rejected to resolved and it will then look like a successful promise when you actually had an error and any caller will think everything was successful. This is because the promise infrastructure assumes that if you .catch() an error and return a value that you have "handled" the error and the state is now resolved successfully. To allow the error to continue to propagate to higher callers from a .catch(), you have to re throw or return a rejected promise:

blah(...).then(...)
.catch(function(e)) {
     throw new Error(res.json(error: e));
});

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to return response to client in the middle of Promise?

From Dev

How to make $http.get return response instead of promise object?

From Dev

How to return a failed promise?

From Dev

difference in how to return a promise?

From Dev

how to return a nested promise?

From Dev

How to return a promise?

From Dev

How to return a JQuery promise

From Dev

how to get the promise value of return promise

From Dev

webapi 2 - how to properly invoke long running method async/in new thread, and return response to client

From Dev

How to return middle records in rails db?

From Dev

How to confirm client received response

From Dev

How to return data from promise

From Dev

How to return a promise with ngresource post

From Dev

How to return a proper Promise with TypeScript

From Dev

How to return value in promise javascript

From Dev

How to return the result of a nested promise?

From Dev

How is RETURN applied in a nested Promise

From Dev

How to return data along with a promise

From Dev

How to return a Promise Function in For loop?

From Dev

How can I return a Promise?

From Dev

return response for module.export from promise chain function

From Dev

Ember- return the response from a nested promise in a closure action

From Dev

How to return response of array of objects

From Dev

How to return a json response in twisted?

From Dev

How to return Guzzle JSON response

From Dev

How to return a response via php

From Dev

How to return an HTTP response in Javascript?

From Dev

How to test component behaviour in response to promise

From Dev

How to test component behaviour in response to promise