Promise.map 未完成,因为后续 Promise.join 先完成? Promise.all?

Promise.map not finishing because subsequent Promise.join finishes first? Promise.all?

我还没有兑现诺言..

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

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

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

这是 Promise 系列(映射、映射、映射、连接):

现在,Promise.join 在 'items' 地图完成 运行 之前保存并完成,因此 'senders' 地图不包含 'itemsArray' javascriptObject on save .. 如何解决这个问题?

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...});

是的,这可以大大简化,尽管您的问题可能只是忘记 return 第一个映射器的任何内容(以及无用的 fulfillmentValue 代码的巨大荒地)。

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");
});