How to pass arguments from the previous then state

2019-09-13 17:11发布

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.

1条回答
冷血范
2楼-- · 2019-09-13 18:14

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');
                }
查看更多
登录 后发表回答