1

コールバック更新クエリを素敵できちんとした約束に変換しようとしています..しかし、それは私を襲いました。そもそも約束する必要がありますか?

これが私の古いコールバック関数です。

con.getConnection(function(err,connection){
    if (err) console.log("Get Connection Error.. "+err);
    con.query("UPDATE player_data SET x="+mysql.escape(x)+", y="+mysql.escape(y)+" WHERE id="+mysql.escape(this.id),function(err) {
        if (err) console.log(err);
        this.x = x;
        this.y = y;
        connection.release();
    });
    req.io.emit("talk", {x:req.data.x,y:req.data.y});
    console.log(this.x,this.y);
});

これは確かに機能しますが、それは約束と同じくらい良いのでしょうか? プロミスは更新/挿入クエリで機能しますか? 以下の関数はエラーなしで機能していません。これにより、プロミスがデータベース データの更新または単に選択に使用されるのか疑問に思います。または、関数が壊れているだけで、エラーが壊れていませんか?

con.getConnectionAsync().then(function(connection) {
    console.log(x,y,this.id) //params sent in. x/y change often, but id = 9. My console shows the values
    connection.queryAsync("UPDATE player_data SET x="+mysql.escape(x)+", y="+mysql.escape(y)+" WHERE id="+mysql.escape(this.id)) //does not update db!
    .finally(function() {
        this.x = x; //this.x logs x
        this.y = y; //same with y
        req.io.emit("talk", {x:this.x,y:this.y});
            //the socket is logged on another page, logs the same data as above.
        connection.release();
        console.log(this.x,this.y); //still logs
    });
});
4

1 に答える 1