Below, you'll see where i'm having an issue in the commented code. I have a nested promise which creates an object in a collection, then returns it.
But, I think i'm having an issue with async. The function completes before the created object is returned by the nested promise.
I'm still grasping promise chaining, and i'm probably doing a lot wrong here. Much of this may be cleared up if I could clean / flatten some of this.
PostSchema.statics.createPost = function (o, user) {
var whiskey;
return Whiskey
.createWhiskey(o.whiskey.value)
.then(function (whiskeyData) {
whiskey = whiskeyData.whiskey;
o.post.whiskey = whiskeyData.whiskey._id;
if (whiskeyData.whiskey.distiller) {
o.post.distiller = whiskeyData.whiskey.distiller;
}
return o.distiller.new === true && !whiskey.distiller ? Distiller.newDistiller(o.distiller.value) : Promise.resolve()
.then(function (distiller) {
//this never invokes <---- it's called from the function below
console.log('never invokes', distiller).
if (distiller) {
whiskey.distiller = distiller._id;
//test this save
whiskey.save();
o.post.distiller = distiller._id;
}
var post = o.post;
post.user = user._id;
return Post
.createAsync(post)
.then(function (data) {
return Post
.populate(data, {
path: 'user whiskey',
populate: {
path: 'distiller style',
}
})
})
.then(function (populatedData) {
return (user.shareFB ? social.checkFB(user, populatedData) : Promise.resolve())
.then(function (FBres) {
return (user.shareTWT ? social.checkTWT(user, populatedData) : Promise.resolve())
.then(function (TWTres) {
var socialData = [TWTres, FBres];
return {
'post': populatedData,
'social': socialData
};
})
})
})
})
})
.catch(function (err) {
console.log('post create err : ', err);
})
};
this is where the distiller is created and attempting to return:
DistillerSchema.statics.newDistiller = function (o) {
return Distiller
.findAsync({
'name': o.name
})
.then(function (distiller) {
if (distiller.length) {
return distiller[0];
}
return Distiller
.createAsync(o)
.then(function (data) {
//console.log here indicates that is is created <-- created and returned here
console.log('distiller created ', data)
return data;
})
})
.catch(function(err) {
console.log('create distiller err ', err);
})
};