Promise.map没有完成,因为后续Promise.join首先完成? Promise.all?

时间:2015-02-12 15:59:37

标签: javascript node.js mongoose promise bluebird

我仍然掌握着承诺......

以下是所涉及的数据库集合的模型:

var itemSchema = new Schema({
  label : String,
  tag : { "type": Schema.ObjectId, "ref": "tag" }
});

var tagSchema = new Schema({
  label : String,
});

这里是Promise系列(地图,地图,地图,连接):

现在,Promise.join在'项目'之前保存并完成。地图完成运行,所以发送者'地图不包含' itemsArray'保存时的javascriptObject ..如何解决?

var reqItems = req.body.items;
var itemsArray = [];
var items = Promise.map(reqItems,function(element){
    var existingItem = Models.Item.findOneAsync({ "label": element });
        existingItem.then(function (value) {
        if ( (existingItem.fulfillmentValue != null) ) { // if this item exists
            var itemObject = [
                                { "item" : existingItem.fulfillmentValue._id },
                                { "label" : existingItem.fulfillmentValue.label }
                                { "tag" : existingItem.fulfillmentValue.tag }
                            ];
            itemsArray.push(itemObject);
        } else { // first instance of this item .. create newItem
            var existingTag = Models.Tag.findOneAsync({ "label": element });
                existingTag.then(function (value) {
                if ( (existingTag.fulfillmentValue != null) ) { // if this tag exists
                    var newItem = new Models.Item(
                        {
                          label : element,
                          tag : existingTag.fulfillmentValue._id,
                        }
                    );
                    newItem.save(function (err) { // save the newItem with existing tag
                        console.log(err);
                        var newSavedItem = Models.Item.findOneAsync({ "label": element });
                        newSavedItem.then(function (value) { // ensure existence of newItem
                            var itemObject = [
                                                { "item" : newSavedItem.fulfillmentValue._id },
                                                { "label" : newSavedItem.fulfillmentValue.label },
                                                { "tag" : newSavedItem.fulfillmentValue.tag }
                                            ];
                            itemsArray.push(itemObject); // push item to array
                        });
                    });
                } else { // else this tag does not exist
                        var newTag = new Models.Tag(
                            {
                              label : element
                            }
                        );
                        newTag.save(function (err) {
                            console.log(err);
                            var newSavedTag = Models.Tag.findOneAsync({ "label": element });
                            newSavedTag.then(function (value) { // ensure existence of newTag
                                if ( (newSavedTag.fulfillmentValue != null) ) {
                                    var newItem = new Models.Item(
                                        {
                                          label : element,
                                          tag : newSavedTag.fulfillmentValue._id,
                                        }
                                    );
                                    newItem.save(function (err) {
                                        console.log(err);
                                        var newSavedItem = Models.Item.findOneAsync({ "label": element });
                                        newSavedItem.then(function (value) { // ensure existence of newItem
                                            var itemObject = [
                                                                { "item" : newSavedItem.fulfillmentValue._id },
                                                                { "label" : newSavedItem.fulfillmentValue.label },
                                                                { "tag" : newSavedItem.fulfillmentValue.tag }
                                                            ];
                                            itemsArray.push(itemObject); // push item to array
                                        }); // newSavedItem.then
                                    }); // newItem.save
                                } // if newSavedTag.isFulfilled
                            }); // newSavedTag.then
                        }); // newTag.save
                } // else tag does not exist
                }); // existingTag.then
          } // first instance of this item .. create newItem
        }); // existingItem.then
    itemObject = null; // reset for map loop
}); // Promise.map itemsArray


var receivers = Promise.map(receiverArray,function(element){
    return Promise.props({
        username : element
    });
});
var senders = Promise.map(senderArray,function(element){
    return Promise.props({
        username : element,
        items : itemsArray
    });
});
Promise.join(receivers, senders, function(receivers, senders){
    store.receivers = receivers;
    store.senders = senders;
    var saveFunc = Promise.promisify(store.save, store);
    return saveFunc();
}).then(function(saved) {
    console.log(saved);
    res.json(saved);
})...error handling...});

1 个答案:

答案 0 :(得分:2)

是的,可以大规模简化,虽然您的问题可能只是忘记将任何内容返回到第一个映射器(以及无用履行值代码的大量荒地)。

var reqItems = req.body.items;
var items = Promise.map(reqItems, function(element) {
    return Models.Item.findOneAsync({ "label": element }).then(function(item) {
        if (item != null) {
            return item;
        } else {
            return Models.Tag.findOneAsync({ "label": element }).then(function(tag) {
                if (tag == null) {
                    var newTag = new Models.Tag({label: element});
                    return newTag.saveAsync().then(function() {
                        return Models.Tag.findOneAsync({ "label": element });
                    })
                }
                return tag;
            }).then(function(tag) {
                var newItem = new Models.Item({
                    label: element,
                    tag: tag._id
                });
                return newItem.saveAsync().then(function() {
                    return Models.Item.findOneAsync({ "label": element });
                });
            })
        }
    });
});

var receivers = Promise.map(receiverArray, function(element){
    return Promise.props({
        username : element
    });
});
var senders = Promise.map(senderArray, function(element){
    return Promise.props({
        username : element,
        items : itemsArray
    });
});

Promise.join(receivers, senders, items, function(receivers, senders, items) {
   store.receivers = receivers;
   store.senders = senders;
   store.items = items;
   return store.saveAsync().return(store);
}).then(function(store) {
   console.log("saved store", store);
   res.json(store);
}).catch(Promise.OperationalError, function(e) {
   console.log("error", e);
   res.send(500, "error");
});