请问如何将result从bluebird的chain中返回出来
发布于 2 个月前 作者 oMaten 179 次浏览 来自 问答

刚刚上手bluebird,请问在下面这种情况下如何将findUser中的user返回出来?

userSchema.statics.signin = function(data){
	var user = this.findUser(data);
	// Using user and data here...
	
};

userSchema.statics.findUser = function(data){
	this.findOneAsync({ username: data.username })
		.then(function(user){
			// want to return user
		})
		.error(function(error){
			console.error(error.message);
		});
};

官方文档中的例子好像并不适用

var Promise = require("bluebird");
var fs = Promise.promisifyAll(require("fs"));
var baseDir = process.argv[2] || ".";
function writeFile(path, contents) {
    var fullpath = require("path").join(baseDir, path);
    return fs.writeFileAsync(fullpath, contents).thenReturn(fullpath);
}writeFile("test.txt", "this is text").then(function(fullPath) {
    console.log("Successfully file at: " + fullPath);
});
4 回复

采用mongoose promise实现:

var User = mongoose.model(‘User’);
User.findOne({username: data.username}).exec().then(function(user){
	// do something with user
	return user.save();
}).then(function(user){
	// do something
	return res.json(user);
}).then(undefined,function(err){
	// deal err
});

@j60017268 我这个只是一个栗子 就是想问一下这种情况下bluebird如何能够将then中传递的参数return出来

@magicdawn then里面直接return?不行吧

回到顶部