如何将参数从上一个then语句传递到下一个 [英] How to pass arguments from the previous then statement to the next one

查看:700
本文介绍了如何将参数从上一个then语句传递到下一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在玩Promises时遇到了一个问题.

I am having fun with Promises and have one problem.

这是我的代码

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

已将book参数设置为undefined.

Has the book argument set to undefined.

我做错了什么,如何将结果从then语句传递到下一个语句.

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

谢谢.

推荐答案

对不起,我刚发现问题,您只需要抛出一个错误就可以打破链条.

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

这篇关于如何将参数从上一个then语句传递到下一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆