How to pass arguments from the previous then statement to the next one

谁都会走 提交于 2019-12-25 09:26: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:


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');
                }


来源:https://stackoverflow.com/questions/43897272/how-to-pass-arguments-from-the-previous-then-statement-to-the-next-one

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!