[英]How to pass a third argument to a callback using Bluebird.js nodeify
With a little help I've arrived at the following code to promisify a passport.js login strategy. 在一点点帮助下,我已经得到以下代码来宣传passport.js登录策略。
var passport = require('passport');
var LocalStrategy = require('passport-local').Strategy;
var Promise = require('bluebird');
var bcrypt = require('bcrypt');
var db = require('./db').db; //users are stored in mongo
//I'm using bluebird.js for promises
var users = Promise.promisifyAll(db.users);
var compare = Promise.promisify(bcrypt.compare);
// This strategy is used by passport to handle logins
module.exports.localStrategy = new LocalStrategy(function(username, password, done) {
users.findOneAsync({username: username}).bind({})
.then(function(user) {
if (!user) {
throw new NoMatchedUserError('Incorrect username.');
//should be equivalent to:
// return done(null, false, {message:'something'});
}
this.user = user;
return compare(password, user.password);
})
.then(function(isMatch) {
if (isMatch) {
return this.user;
//is equivalent to:
// return done(null, this.user);
}
else {
throw { message: 'Incorrect password.' };
//should be equivalent to:
// return done(null, false, {message:'something else'};
}
})
.nodeify(done);
});
by calling nodeify(done) I can handle the path where passwords match but I don't know how to pass the optional third parameter out so that passport.js can use it. 通过调用nodeify(完成)我可以处理密码匹配的路径,但我不知道如何传递可选的第三个参数,以便passport.js可以使用它。
Is it possible to have the two failure (not error) paths handled? 是否可以处理两个故障(非错误)路径?
Update: 更新:
As asked in the comments I created an issue on Github and this feature was (very promptly) added in Bluebird v2.0 正如评论中所提到的,我在Github上创建了一个问题,并且这个功能(非常及时地)在Bluebird v2.0中添加了
https://github.com/petkaantonov/bluebird/issues/219 https://github.com/petkaantonov/bluebird/issues/219
Use: 采用:
.nodeify(done, {spread: true});
This allows multiple arguments to be passed to the 'done' callback. 这允许将多个参数传递给'done'回调。
More info on: 更多信息:
I'm adding this answer to show how to use .nodeify(done, {spread: true})
(as mentioned in other answers/comments) with the original example. 我正在添加这个答案,以展示如何使用
.nodeify(done, {spread: true})
(如其他答案/评论中所述)与原始示例。
var passport = require('passport');
var LocalStrategy = require('passport-local').Strategy;
var Promise = require('bluebird');
var bcrypt = require('bcrypt');
var db = require('./db').db; //users are stored in mongo
//I'm using bluebird.js for promises
var users = Promise.promisifyAll(db.users);
var compare = Promise.promisify(bcrypt.compare);
// This strategy is used by passport to handle logins
module.exports.localStrategy = new LocalStrategy(function(username, password, done) {
users.findOneAsync({username: username}).bind({})
.then(function(user) {
if (!user) {
return [false, { message: 'Incorrect username.' }]; <---------------
//should be equivalent to:
// return done(null, false, {message:'something'});
}
this.user = user;
return compare(password, user.password);
})
.then(function(isMatch) {
if (isMatch) {
return this.user;
//is equivalent to:
// return done(null, this.user);
}
else {
return [false, { message: 'Incorrect password.' }]; <---------------
//should be equivalent to:
// return done(null, false, {message:'something else'};
}
})
.nodeify(done, {spread: true});
});
Currently, there is no way to do it with .nodeify
, you can of course do it manually with .then
: 目前,还没有办法做到这一点的
.nodeify
,你当然可以用做手工.then
:
.then(function(result){
done(/*whatever arguments you need*/);
},function(failure){
done(/* failure argumnets */);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.