I am using Sequelize, which uses Bluebird promises and need to iterate over an array of objects (inserting them one by one - bulkCreate cannot handle duplicate checks on mssql).
It looks something like this:
var users = [.....] // Filled up somewhere else.
Product.create(model)
.then(function() {
return users;
})
.each(function(user) {
return User.create(......);
})
My question is: Is it ok to return an array of stuff (not promises) like I do in the then?
EDIT: Another example
Here is another, better, example of that I was trying to do (with the exception of that this is a setTimeout() instead of a database write). And it looks like it works. It takes each element in the array (two, three, four) and executes the function send to each().
var Promise = require("bluebird");
function myDelay(who) {
var deferred = Promise.pending();
console.log("Starting: " + who);
setTimeout(function() {
console.log("Done with: " + who);
deferred.resolve();
}, 250);
return deferred.promise;
}
myDelay("one")
.then(function() {
return ["two", "three", "four"];
})
.each(function(who) {
return myDelay(who);
})
.then(function() {
console.log("All done!");
});
It looks to me like it works fine. The output looks like this:
Starting: one
Done with: one
Starting: two
Done with: two
Starting: three
Done with: three
Starting: four
Done with: four
All done!
with a small delay between each "number".