【发布时间】:2017-08-01 01:58:52
【问题描述】:
我需要建立map_of_uuid_ids_and_field_names_to_an_array_of_field_values。
我需要在循环完成循环后返回这个对象。如何?
现在我的代码挂在这个循环的内部。我看了看,发现内部的“then()”语句没有“return”语句。但是,当我需要循环代码时,我怎么能在那里放一个 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;
});
}
【问题讨论】:
-
长名字到底是怎么回事?!
-
如果它是异步的,你就不能真正返回。
-
这是一个有趣的问题:)
-
另外,您应该重新评估您的命名策略。明确变量名是好的,但这是越界了。