var newUser;
newUser = {
name: req.body.name,
email: req.body.email,
username: req.body.username,
password: req.body.password
};
global.db.User.create(newUser).then(function(dbUser) {
var newEntity;
newEntity = {
name: newUser.name + " Default Entity"
};
return global.db.Entity.create(newEntity);
}).then(function(dbEntity) {
console.log(dbEntity);
return res.json(dbUser.display());
})["catch"](function(err) {
console.log(err);
return next(err);
});
global.db
是一個Sequelize對象,它有User
模型和Entity
模型。我想先創建一個用戶,然後爲該用戶創建一個實體。這是將事情鏈接在一起讓承諾有用的正確方法嗎?這是在Sequelize.js中使用promise的正確方法嗎?
您可能還想閱讀http://blog.slaks.net/2015-01-05/introducing-promises – SLaks 2015-02-09 19:56:33
@SLaks不錯的文章!它有幾個(小的)錯誤 - 我在哪裏報告給他們?此外 - 因爲承諾現在有一個相當固定的創建語法,你可能想使用該語法(承諾構造函數,而不是延期) – 2015-02-10 08:39:18
@BenjaminGruenbaum:謝謝!我提到承諾構造函數,在延期後。請注意,這不僅僅是關於JS承諾。您可以在博客文章留言,甚至發送拉請求(https://github.com/SLaks/SLaks.Blog/blob/gh-pages/_posts/2015-01-05-introducing-promises.md) – SLaks 2015-02-10 16:07:37