如何将参数从上一个 then 语句传递到下一个语句



我玩得很开心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((book) => {
                    book.save();
                })

将 book 参数设置为未定义。

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

谢谢。

抱歉,我刚刚发现了问题,您只需抛出错误即可断开链条。

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

最新更新