如何使用蓝鸟承诺将parameter passing给护照策略callback?

我正在努力promisify passport.js的本地策略 。 我对诺言和护照都很陌生,而且我非常依赖这个评论线程 ,它使用蓝鸟的Promise库来传递额外的参数给passport的done()callback函数。 这个评论导致了一个新的蓝鸟实现处理额外的callback参数,但我不能让它在我自己的代码中工作:

 const passport = require('passport'); const User = require('../models/user'); const LocalStrategy = require('passport-local'); const NoMatchedUserError = require('../helpers/error_helper').NoMatchedUserError; const NotActivatedError = require('../helpers/error_helper').NotActivatedError; const localOptions = { usernameField: 'email' }; const localLogin = new LocalStrategy(localOptions, function(email, password, done) { let user; User.findOne({ email: email }).exec() .then((existingUser) => { if (!existingUser) { throw new NoMatchedUserError('This is not a valid email address.'); } user = existingUser; return user.comparePassword(password, user.password); }) .then((isMatch) => { if (!isMatch) { throw new NoMatchedUserError('This is not a valid password.'); } return user.isActivated(); }) .then((isActivated) => { if (!isActivated) { throw new NotActivatedError('Your account has not been activated.'); } return user; }) .asCallback(done, { spread: true }); }); 

用户能够validation没有问题。 这是身份validation失败,我有一个问题: done(null, false, { message: 'message'}显然不是在.asCallback方法中调用。我敢肯定,这是抛出一个错误,所以我试着用这个:

 if (!existingUser) { return [ false, { message: 'This is not a valid email address.' } ]; } 

但是返回一个数组也是行不通的,因为它传递给承诺链并破坏了代码。

有什么想法吗?