6
我有一個類中我用下面的代碼的NodeJS應用:承諾正在等待
var mongoose = require('mongoose');
var Roles = mongoose.model('roles');
var Promise = require("bluebird");
module.exports = Role;
var err = null;
var id;
function Role(name, companyId) {
this.err = err;
this.name = name;
this.companyId = companyId;
this.id = getId(name, companyId);
}
var getId = function (name, companyId) {
return new Promise(function(resolve, reject) {
Roles.findOne({companyId:companyId, name:name}, function(err,result) {
resolve(result._id);
});
});
};
當我調用類中,ID正在等待:
var currentRole = new Role(myRole, comId);
console.log(currentRole);
我如何獲得解決這個問題時的價值觀?
當出現'err'時,應該'拒絕'。 – Bergi
看看[是不是很好的做法,有一個構造函數返回一個Promise?](http://stackoverflow.com/q/24398699/1048572) - 你可能不想做'currentRole.id.then (...)' – Bergi