Last active
August 17, 2016 03:36
-
-
Save tzechienchu/65ef0e02adfff6b401c3cb50bbfe1b2f to your computer and use it in GitHub Desktop.
PromiseAndCallBack
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 修改后 既支持 callback 回调,又支持 Promise | |
var getInfo = function(uid, callback) { | |
return new Promise(function(resolve, reject) { | |
if (callback) { | |
resolve = function (ret) { | |
callback(null, ret); | |
}; | |
reject = callback; | |
} | |
var sql = "select * from v_user where uid=? limit 1"; | |
Mysql.query(sql, [uid], function(err, row) { | |
if(err) return reject(err); | |
if(row.length===0) return resolve(); | |
var info = row[0]; | |
resolve(info); | |
}); | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment