javascript - catch rejection in $q service without triggering success callbacks -
i have method returns $q
(q) promise:
var subtypesmetadataresolved = restservice.getnodesubtypesmetadata();
now, when metadata available, want run 2 functions process them. first, thought chain them this:
subtypesmetadataresolved.then(createnodes).then(preparedataforwidgets)
but realized since both require data returned subtypesmetadataresolved
promise need return data createnodes
success callback it's passed preparedataforwidgets
, not option. made this:
subtypesmetadataresolved.then(createnodes) subtypesmetadataresolved.then(preparedataforwidgets)
which works need. problem how rejection
callback called when subtypesmetadataresolved
rejected , don't neither createnodes
nor preparedataforwidgets
callbacks triggered in case? have following, after triggering nodesubtypesmetadataerrorcb
triggers createnodes
callback:
subtypesmetadataresolved.catch(nodesubtypesmetadataerrorcb); subtypesmetadataresolved.then(createnodes) subtypesmetadataresolved.then(preparedataforwidgets)
here how reject subtypesmetadataresolved
:
edit:
function getnodesubtypesmetadata(subtype) { return $q.when("success!").then(function(){ debugger throw new error(); }); } var subtypesmetadataresolved = getnodesubtypesmetadata(); subtypesmetadataresolved.then(successcb1); subtypesmetadataresolved.then(successcb2); subtypesmetadataresolved.catch(nodesubtypesmetadataerrorcb); $q.all([ typesmetadataresolved, subtypesmetadataresolved ]).then(init);
the problem assigning promise handled error subtypesmetadataresolved
. if call .then()
on this, will call then()
callbacks because .catch()
callback "handling" error.
to solve this, assign unhandled promise variable, , call .catch()
/.then()
on that:
var subtypesmetadataresolved = getnodesubtypesmetadata(); subtypesmetadataresolved.catch(nodesubtypesmetadataerrorcb); subtypesmetadataresolved.then(function(){ debugger }); subtypesmetadataresolved.then(function () { debugger });
as matter of style, suggest placing catch
line after then
lines, should have no observable effect on code's behavior.
Comments
Post a Comment