Cleaning up nested promises in Cloud function I am writing the Cloud function for Parse whose purpose is to update my database with a provided array of data objects.  The main problem are the nested promises, which doesn't look right.  Any help to refactor this code or any other critiques?

/** 
 * Updating multiple object arrays: 
 * Look for object with the same idKey and update with supplied data or create new if none is found
 * Expect Data {schema: &lt;schema_name&gt;, idKey: &lt;name_of_id_key&gt;, data: &lt;object_array&gt;}
 */

Parse.Cloud.define("updater", function(request, response) {
    var _ = require('underscore');

    var schema          = request.params.schema,
            idKey       = request.params.idKey,
            objectArray = request.params.data;

    var Class           = Parse.Object.extend(schema),
            parseObject, 
            id,
            query;

    _.each(objectArray, function(dataObject){
            id = dataObject[idKey];
            if (!id) return response.error("Supplied Object " + dataObject + " has no id Key '" + idKey + "' set!");

            // now query for the same idKey
            query           = new Parse.Query(Class);
            query.equalTo(idKey, id);
            query.find().then(
                function(array){
                    if(array) {
                        // found at least one object - take the first
                        parseObject = array[0];

                        // use the new dataObject to update parseObject
                        parseObject.set(dataObject);
                        parseObject.save().then(

                            /*
                             * should this go outside the promise?
                             */

                            function(object){
                                // success
                            },
                            function(error){
                                return response.error(error);
                            });                 
                    } else {
                        // nothing found - create new
                        parseObject = new Class(dataObject);
                        parseObject.save().then(
                            function(object){
                                // success
                            },
                            function(error){
                                return response.error(error);
                            });                 
                    }
                }
            );

            parseObject = new Class(dataObject);

            parseObject.save().then(
            function(object){
                // success
            },
            function(error){
                response.error(error);
            });
    });

    response.success("Success!");

});
I'd start with refactoring the promises functions and remove any code repetition, something like this:

...

function success(object){
    // success                          
}
function error(error){
    return response.error(error);
}
...

query.find().then(function(array){
    if(array) {
        // found at least one object - take the first
        parseObject = array[0];        
        parseObject.set(dataObject);        
     } else {
        // nothing found - create new
        parseObject = new Class(dataObject);
     }    
     parseObject.save().then(success,error); 
});
...