Promise.map not finishing because subsequent Promi

2019-09-17 04:57发布

问题:

Am still getting the hang of promises..

Here are the models of the db collections involved:

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

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

And here's the series of Promises (map, map, map, join):

Right now, the Promise.join saves&completes before the 'items' map finishes running, and so the 'senders' map doesn't include the 'itemsArray' javascriptObject on save .. how can this be resolved?

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:

Yes that can be massively simplified, although your problem was probably just forgetting to return anything to the first mapper (and the massive wasteland of useless fulfillmentValue code).

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