Heim  >  Fragen und Antworten  >  Hauptteil

node.js – So sorgen Sie dafür, dass CO in der Funktion getByName() nach der Ausführung zurückkehrt

Benutzer-Login in Express ermitteln (mit Sequelize und Co)
Die Informationen in console.log werden jedoch erst nach Return Res gesehen. Wie kann die Funktion in co nach der Ausführung zurückgegeben werden?

给我你的怀抱给我你的怀抱2713 Tage vor677

Antworte allen(2)Ich werde antworten

  • 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)
    })

    Antwort
    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
        })
    }

    Antwort
    0
  • StornierenAntwort