Skip to content

Instantly share code, notes, and snippets.

@mosluce
Last active December 27, 2015 13:56
Show Gist options
  • Save mosluce/90a711ad19a12ec55599 to your computer and use it in GitHub Desktop.
Save mosluce/90a711ad19a12ec55599 to your computer and use it in GitHub Desktop.
//npm install --save bluebird
var Promise = require('bluebird');
Check.updataPartly = function(foodName, partly, numBig, numSamll, callback){
//打開資料庫
mongodb.open(function(err,db){
if (err) {
return callback(err);
}
//讀取checks的集合
db.collection('checks',function(err,collection){
if (err) {
mongodb.close();
return callback(err);
}
//蒐集非同步處理工作
var tasks = [];
var task = function(i) {
return new Promise(function(resolve, reject) {
collection.update({
"foodName":foodName[i]
},{//修改
$set: {partly: partly-(numSamll[i]+numBig[i])}
},function(err) {
mongodb.close();
if (err) {
return reject(err);
}
resolve(null);
});
});
}
for (var i = 0; i < foodName.length; i++) {
tasks.push(task(i));
}
Promise.all(tasks).then(function() {
callback(null);
}, function(err) {
callback(err);
});
});
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment