Created
July 10, 2017 14:53
-
-
Save mkjiau/caf1fa5797d32f22a15f46854d2fcb1c to your computer and use it in GitHub Desktop.
This file contains hidden or 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
db.accounts.findById(id, function(error, user) { | |
//....AAAA | |
db.accounts.useUsertoDo(user, function(error, something) { | |
// use something to do something | |
}); | |
}); | |
//------------------------------ | |
var promise1 = db.accounts.findById(id); | |
var promise2 = user => { | |
return db.accounts.useUsertoDo(user); | |
}; | |
promise1 | |
.then(user => { | |
//....AAAA | |
return promise2(user); | |
}) | |
.then(something => { | |
// use something to do something | |
}) | |
.catch(error => {}); | |
//------------------------------ | |
var promises = []; | |
promises.push(db.accounts.findById(id)); | |
promises.push(db.accounts.findByName1(name)); | |
promises.push(db.accounts.findByName2(name)); | |
var promiseLast = (id, name1, name2) => { | |
return db.accounts.compareNameAndId(id, name1, name2); | |
}; | |
Promise.all(promises) | |
.then(values => { | |
var id = values[0]; | |
var name1 = values[1]; | |
var name2 = values[2]; | |
return promiseLast(id, name1, name2); | |
}) | |
.then(something => {}) | |
.catch(error => {}); | |
// --------------------- | |
function findById(_id, cb) { | |
//.... | |
if (error) { | |
cb(error, null); | |
} else { | |
cb(null, data); | |
} | |
} | |
// ---------------------- | |
function findById(_id) { | |
return new Promise(function(resolve, reject) { | |
//.... | |
if (error) { | |
reject(error); | |
} else { | |
resolve(data); | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment