我需要建立map_of_uuid_ids_and_field_names_to_an_array_of_field_values
。如何從NodeJS的循環中返回Promise?
我需要在循環完成循環後返回此對象。怎麼樣?
現在我的代碼掛在這個循環的內部。我看着它,意識到內部的「then()」語句沒有「return」語句。但是,當我需要代碼循環時,我該如何在那裏放置返回語句?我如何從這個內部循環返回一個Promise?
function get_map_of_uuid_ids_and_field_names_to_an_array_of_field_values(string_from_api_call, api_key_whose_name_should_match_the_name_of_a_database_field) {
return new Promise(function(resolve, reject){
var map_of_uuid_ids_and_field_names_to_an_array_of_field_values = {};
sanitized_string_from_api_call = database_queries.sanitize_string(string_from_api_call);
// 2017-07-10 -- this fixes:
// TypeError: Cannot read property 'split' of null, at get_map_of_uuid_ids_and_field_names_to_an_array_of_field_values (/home/ec2-user/daemons/deduplication_api/v9/dupe-res/actions.js:456:82)
if (sanitized_string_from_api_call) {
var array_of_words_from_string_from_api_call = sanitized_string_from_api_call.split(/\s+/);
for (var k in array_of_words_from_string_from_api_call) {
var word = array_of_words_from_string_from_api_call[k];
// 2017-02-27 -- for the sake of performance, we skip over any string of 2 letters or less.
// This means headquarters_country_code and headquarters_state_code need special handling.
if (word.length > 2) {
return database_queries.get_map_of_type_of_profile_and_profile_id_pointing_to_document(word)
.then(function(map_of_type_of_profile_and_profile_id_pointing_to_document) {
if (map_of_type_of_profile_and_profile_id_pointing_to_document) {
map_of_uuid_ids_and_field_names_to_an_array_of_field_values = merge_objects(map_of_uuid_ids_and_field_names_to_an_array_of_field_values, transform_map_of_profile_type_and_profile_id_to_map_of_uuid_to_documents(map_of_type_of_profile_and_profile_id_pointing_to_document));
}
});
}
}
} else {
console.log("the string value was null when api_key_whose_name_should_match_the_name_of_a_database_field was : " + api_key_whose_name_should_match_the_name_of_a_database_field);
}
return map_of_uuid_ids_and_field_names_to_an_array_of_field_values;
});
}
準確的長名字是什麼?! – Vandesh
如果它是異步的,你不能真正返回。 –
這是一個有趣的問題:) –