Home  >  Q&A  >  body text

node.js - How to make co in function getByName() return after execution

Determine user login in express (using sequelize and co)
However, the information in console.log is only seen after return res. How to make the function in co return after executing it?

给我你的怀抱给我你的怀抱2713 days ago676

reply all(2)I'll reply

  • PHP中文网

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

    Generally, a Promise is returned, and it must be thened wherever used.

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

    reply
    0
  • 过去多啦不再A梦

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

    UserBil.getByName = () => {
        var result = co.wrap(function* (){
            return request
            // something you want to return 
        })
        
        return result(true).then(function(value){
          return value
        })
    }

    reply
    0
  • Cancelreply