Ho un problema con jQuery 1.9.1 promesse, in cui potenzialmente ho bisogno di una logica condizionale che restituirà un altro rinviato e non sono sicuro di come gestirlo. Questo è stato il mio miglior tentativo, ma come i commenti indicano di seguito, quando premo il ramo else, colgo comunque la seconda funzione .then(), dove spero di poter tornare all'utente. Qualche modello per come gestire un tale scenario?Gestione di filiali con Promises
storage.provision(c)
.then(function(rc){
if(rc === 0){
storage.write(c);
}else{
return options.onSuccess(rc); //how i got back to the users callbacks/promise, but this
//takes me to the .then below
}
})
//storage.write returns a promise as well, do I do another .then
// like this?
.then(function(rc){
//I was hoping this would catch, the storage.write() case, and it does, but it also catches
//the retun options.onSuccess(rc) in the else case.
options.onSuccess(rc);
})
.fail(function(e){
//handle error using .reject()
});
Questo post potrebbe aiutarti: http://stackoverflow.com/questions/12149993/attempting-to-break-jquery-promise-chain-with-then-fail-and-reject – mattytommo
[questo post] (http: //net.tutsplus.com/tutorials/javascript-ajax/wrangle-async-tasks-with-jquery-promises/) ti aiuterà a capire meglio le promesse. In generale, se vuoi usare i condizionali dovresti salvare la promessa come variabile ed eseguire il tuo 'success'' o' fallire' nei tuoi condizionali, sembra che tu stia facendo il contrario. – acconrad