如何使用Bluebird.js nodeify将第三个参数传递给回调 [英] How to pass a third argument to a callback using Bluebird.js nodeify

查看:151
本文介绍了如何使用Bluebird.js nodeify将第三个参数传递给回调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一点帮助下我已经找到了以下代码来说明 passport.js登录策略.

With a little help I've arrived at the following code to promisify a passport.js login strategy.

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

通过调用 nodeify(done)我可以处理密码匹配的路径,但是我不知道如何传递可选的第三个参数,以便passport.js可以使用它.

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.

是否可以处理两条故障(非错误)路径?

Is it possible to have the two failure (not error) paths handled?

更新:

根据评论中的要求,我在Github上创建了一个问题,并且在Bluebird v2.0中(非常迅速地)添加了此功能

As asked in the comments I created an issue on Github and this feature was (very promptly) added in Bluebird v2.0

https://github.com/petkaantonov/bluebird/issues/219

推荐答案

当前,无法使用.nodeify进行此操作,当然可以使用.then手动进行此操作:

Currently, there is no way to do it with .nodeify, you can of course do it manually with .then:

.then(function(result){
     done(/*whatever arguments you need*/);
},function(failure){
     done(/* failure argumnets */);
});

这篇关于如何使用Bluebird.js nodeify将第三个参数传递给回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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