0

I am having fun with Promises and have one problem.

Here is my code

 req.checkBody(BookManager.SCHEME);
        req.getValidationResult()
            .then(function (result) {
                if (!result.isEmpty()) {
                    handler.onError('Invalid payload');
                    return;
                }
                return new BookModel({
                    author: data.author,
                    name: data.name,
                    year: data.year
                });
            })
            .then((book) => {
                book.save();
            })
            .then((saved) => {
                handler.onSuccess(saved);
            })
            .catch((error) => {
                handler.onError(error.message);
            });

But for some reason this then statement

.then((book) => {
                    book.save();
                })

Has the book argument set to undefined.

What I am doing wrong, and how can I pass the result from the then statement to the next one.

Thanks.

  • Sounds like a duplicate of [How do I access previous promise results in a `.then()` chain?](http://stackoverflow.com/q/28250680/1048572) at first, but the actual problem is that you `return;` undefined sometimes from the previous callback. – Bergi May 10 '17 at 17:00

1 Answers1

0

Sorry, I have just found the issue, you need just to throw an error to break a chain.

.then(function (result) {
                if (!result.isEmpty()) {
                       throw new Error('Invalid payload');
                }
jfriend00
  • 683,504
  • 96
  • 985
  • 979