首頁  >  問答  >  主體

node.js - 函數getByName()中如何使得co執行完才return

express中判斷使用者登入(使用了sequelize和co)
但是是return res之後才看到console.log中的資訊。如何使得執行完co中的函數才return?

#
给我你的怀抱给我你的怀抱2713 天前673

全部回覆(2)我來回復

  • PHP中文网

    PHP中文网2017-05-16 13:45:20

    一般是返回個Promise,使用的地方也要then一下。

    UserBLL.GetByName = function(name) {
        return co(function() {
            return yield User.findOne({where: {username: name}})
        })
    }
    
    UserBLL.GetByName('xxx').then(function(res) {
        console.log(res)
    })

    回覆
    0
  • 过去多啦不再A梦

    过去多啦不再A梦2017-05-16 13:45:20

    雷雷

    回覆
    0
  • 取消回覆