Q 承诺链接,错误处理程序未调用
Q promise chaining, error handler not called
考虑这段代码
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 方法似乎可以正常工作。
谁能解释为什么第一个示例不处理承诺链中引发的错误?
我假设 elastic.putSettings()
等 return 是一个承诺。您不能将承诺用作 .then
的参数;该方法需要 function 个参数。反过来,这些功能可以 return 一个承诺。
因此,您需要使用匿名函数包装您的 promise-returning 函数,并将该函数用作 .then
的参数。像这样:
var tryWithoutReindexing = function(indexName, properties) {
var settings = properties["settings"];
var mappings = properties["mappings"];
return elastic.closeIndex(indexName)
.then(function() {
return elastic.putSettings(indexName, settings);
})
.then(function() {
return elastic.putMapping(indexName, mappings);
})
.then(function() {
return elastic.openIndex(indexName);
});
};
考虑这段代码
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 方法似乎可以正常工作。
谁能解释为什么第一个示例不处理承诺链中引发的错误?
我假设 elastic.putSettings()
等 return 是一个承诺。您不能将承诺用作 .then
的参数;该方法需要 function 个参数。反过来,这些功能可以 return 一个承诺。
因此,您需要使用匿名函数包装您的 promise-returning 函数,并将该函数用作 .then
的参数。像这样:
var tryWithoutReindexing = function(indexName, properties) {
var settings = properties["settings"];
var mappings = properties["mappings"];
return elastic.closeIndex(indexName)
.then(function() {
return elastic.putSettings(indexName, settings);
})
.then(function() {
return elastic.putMapping(indexName, mappings);
})
.then(function() {
return elastic.openIndex(indexName);
});
};