Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
How to stop execution if one of the tasks failed?
Message
De
27/03/2018 17:22:17
 
 
Information générale
Forum:
Javascript
Catégorie:
Codage, syntaxe et commandes
Divers
Thread ID:
01659020
Message ID:
01659021
Vues:
40
>Hi everybody,
>
>I have the following code:
>
>
>load($stateParams.draftId);
>
>                getMetaData();
>...
>
>
>The load method has this code:
>
> spinnerService.startSpinner('loaditemtree');
>                   
>                    loadItemTreeRoot(draftId);
>                    loadUnassigned(draftId);
>
>
>The last 2 load functions as well as getMetaData call some service which returns promise. My problem is that I don't want to execute extra code if, for example, the very first method loadItemTreeRoot already returns a bad status.
>
>How can I re-write the above to make sure my methods are executed in the synchronous fashion, to be precise, once one method failed (returned bad request), the rest of my methods are not executed?
>
>Thanks in advance.

Chain them in a boolean expression, connected by && (the first method returning false will break the chain), in the order you want them executed.
load($stateParams.draftId) && getMetaData();

function load (whatever){
  return loadItemTreeRoot(draftId) && loadUnassigned(draftId);
}
getMetaData() will execute only if load() (and, therefore, loadItemTreeRoot() and loadUnassigned()) returns true. If the methods return a non-logical value, add a comparison to the chain to signal success (but the principle remains).
----------------------------------
António Tavares Lopes
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform