-
-
Save freewind/2660993 to your computer and use it in GitHub Desktop.
How to make th
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
var id = 1; | |
function drop(name, callback) { | |
setTimeout(function(){callback(name)}, 3000); | |
} | |
function insert(dataArr, callback) { | |
dataArr.forEach(function(item) { | |
item._id = id++; | |
}); | |
setTimeout(function(){callback(dataArr)}, 1000); | |
} | |
// I want to drop all tables first | |
['aaa','bbb','ccc'].forEach(function(name){ | |
drop(name, function(name){ | |
console.log(name + ' is dropped'); | |
}); | |
}); | |
// !!! how to make sure all tables are dropped before inserting? | |
console.log('all tables are dropped'); | |
// I want to insert some data | |
// !!! how to reduce the callbacks | |
// insert users | |
insert([{name:'a'},{name:'b'}], function(users) { | |
console.log('inserted users'); | |
var user1 = users[0], user2 = users[1]; | |
// insert categories | |
insert([{name:'c', userId:user1._id},{name:'d',userId:user2._id}], function(categories) { | |
console.log('inserted categories'); | |
var category1 = categories[0], category1 = categories[1]; | |
// insert questions | |
insert([{name:'e',userId:user1._id,categoryId:category1._id}], function(questions) { | |
console.log('inserted questions'); | |
var question1 = questions[0]; | |
// insert answers | |
insert([{name:'g', userId:user1._id, questionId: question1._id}], function(answers){ | |
console.log('inserted answers'); | |
var answer1 = answers[0]; | |
// insert comments | |
insert([{name:'i', userId:user1._id, answerId:answer1._id}], function(comments){ | |
console.log('inserted comments'); | |
var comment1 = comments[0]; | |
// insert replies | |
insert([{name:'k',commentId:comment1._id}], function(replies){ | |
console.log('inserted replies'); | |
// do you want more :) | |
console.log('finished'); | |
}); | |
}); | |
}); | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment