Skip to content

Instantly share code, notes, and snippets.

@tzechienchu
Last active August 17, 2016 03:36
Show Gist options
  • Save tzechienchu/65ef0e02adfff6b401c3cb50bbfe1b2f to your computer and use it in GitHub Desktop.
Save tzechienchu/65ef0e02adfff6b401c3cb50bbfe1b2f to your computer and use it in GitHub Desktop.
PromiseAndCallBack
// 修改后 既支持 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