【问题标题】:How to pass arguments from the previous then statement to the next one如何将参数从上一个 then 语句传递到下一个
【发布时间】:2017-10-09 09:14:55
【问题描述】:

我在Promises 上玩得很开心,但遇到了一个问题。

这是我的代码

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

但由于某种原因,这个 then 语句

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

将 book 参数设置为未定义。

我做错了什么,如何将then 语句的结果传递给下一个语句。

谢谢。

【问题讨论】:

标签: node.js asynchronous promise arguments es6-promise


【解决方案1】:

抱歉,我刚刚发现了问题,你只需要抛出一个错误就可以断链。

.then(function (result) {
                if (!result.isEmpty()) {
                       throw new Error('Invalid payload');
                }

【讨论】:

    猜你喜欢
    • 2020-02-23
    • 1970-01-01
    • 2020-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-01
    相关资源
    最近更新 更多