考慮以下代碼Q承諾鏈接,錯誤處理程序不叫
var tryWithoutReindexing = function(indexName, properties) {
var settings = properties["settings"];
var mappings = properties["mappings"];
return elastic.closeIndex(indexName)
.then(elastic.putSettings(indexName, settings))
.then(elastic.putMapping(indexName, mappings))
.then(elastic.openIndex(indexName));
};
而且撥打:
tryWithoutReindexing(indexName, newProperties)
.then(function success(value){
console.log('migration successful');
}, function error(){
console.log('migration unsuccessful');
});
方法elastic.putSettings
拋出錯誤,但由於某些原因,console
日誌'migration is successful'
。我希望錯誤處理程序被調用。
如果我改變的方法是:
var tryWithoutReindexing = function(indexName, properties) {
var settings = properties["settings"];
var mappings = properties["mappings"];
return elastic.closeIndex(indexName)
.then(elastic.putSettings(indexName, settings))
.then(function success() {
console.log('err');
}, function(error) {
console.log(error);
})
.then(elastic.putMapping(indexName, mappings))
.then(elastic.openIndex(indexName));
};
,並把符合console.log(error);
斷點,錯誤處理程序被調用,因此它似乎putSettings法正常工作。
有沒有人可以解釋我爲什麼第一個例子不處理承諾鏈中出現的錯誤?
它似乎並沒有顯示您的實際代碼。你的替代版本的方法有分配'settings'和'mappings'的行,而你的第一個版本沒有這個,但仍然使用這些變量。請將您的代碼削減爲實際產生問題的內容,但是您可以在此修改此內容。我們需要看到實際的代碼,因爲魔鬼在細節中。 – JLRishe
@JLRishe更新,這是唯一的區別,我省略了簡潔 – Raston