我有一个创建deferred
对象的函数。在fail
我调用后备功能时,该功能又会创建并返回它自己的deferred/promise
对象。我想返回此后备的结果-deferred
但我只能error
在初始通话中返回。
这是我在做什么:
// method call
init.fetchConfigurationFile(
storage,
"gadgets",
gadget.getAttribute("data-gadget-id"),
pointer
).then(function(fragment) {
console.log("gotcha");
console.log(fragment);
}).fail(function(error_should_be_fragment) {
console.log("gotcha not");
console.log(error_should_be_fragment);
});
fetchConfiguration
如果我需要的文档/附件不在localstorage中,我的呼叫会尝试从localstorage加载,然后回退到从文件加载。
init.fetchConfigurationFile = function (storage, file, attachment, run) {
return storage.getAttachment({"_id": file, "_attachment": attachment})
.then(function (response) {
return jIO.util.readBlobAsText(response.data);
})
.then(function (answer) {
return run(JSON.parse(answer.target.result))
})
.fail(function (error) {
// PROBLEM
console.log(error);
if (error.status === 404 && error.id === file) {
return init.getFromDisk(storage, file, attachment, run);
}
});
};
我的问题是我可以抓住404
一切,但是error
我不想返回对象,而是想返回由生成的Promise init.getFromDisk
。
问题:
是否可以getFromDisk
在错误处理程序中返回我的调用结果?如果不是,我将如何构造调用的结构,以便始终将诺言返回给我的第一个方法调用?
感谢帮助!
解决方案:
感谢您的帮助!像这样修复它:
init.fetchConfigurationFile(
storage,
"gadgets",
gadget.getAttribute("data-gadget-id"),
pointer
).always(function(fragment) {
console.log("gotcha");
console.log(fragment);
});
init.fetchConfigurationFile = function (storage, file, attachment, run) {
return storage.getAttachment({"_id": file, "_attachment": attachment})
.then(function (response) {
return jIO.util.readBlobAsText(response.data);
})
.then(
function (answer) {
return run(JSON.parse(answer.target.result));
},
function (error) {
if (error.status === 404 && error.id === file) {
return init.getFromDisk(storage, file, attachment, run);
}
}
);
};
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句