下面代码中的Promise可以正常工作,Ajax请求(load() jQuery-function)也可以工作:
changeFragment(newFragment, fragmentUrn) {
new Promise((resolve, reject) => {
this.$MainContent.load(fragmentUrn, (response, status, xhr) => {
//status === 'error' ? reject(xhr) : resolve();
console.log(status == 'success');
if (status === 'success'){
console.log('resolve');
resolve();
}
if (status === 'error') {
console.log('reject');
reject(xhr);
}
});
}).then(() => {
console.log('then');
let newFragment = newFragment.createInstance();
newFragment.initialize();
}).catch((xhr) => {
console.log('Why!?');
console.error(`Error : ${xhr.status} ${xhr.statusText}`);
});
}然而,在then()之后,不知何故也调用了catch()。我在控制台中得到了以下输出:
true
resolve
then
Why!?
Error: undefined undefined发布于 2018-02-12 18:15:03
你需要做这样的事情
Promise.resolve() .then(function() { return Function1().catch(errorHandler1);})
发布于 2018-02-11 18:04:21
我认为因为你没有返回promise,所以尝试返回promise,而不是仅仅用“new”来初始化它。
“‘then”需要等待将返回的承诺
https://stackoverflow.com/questions/48729924
复制相似问题