<note>“Promises provide a well-defined interface for interacting with an object that represents the result of an action that is performed asynchronously, and may or may not be finished at any given point in time.”</note>
…Tout en vous affranchissant de l'écriture du “boilerplate code”
Une promesse est dans l'un des états suivants :
Pending : état initial
Fulfilled : l'opération s'est terminée correctement
Rejected : l'opération s'est terminée avec une erreur
Settled : l'opération s'est terminée, peu importe comment
Création d'une promesse :
var p1 = new Promise(
// The resolver function is called with the ability to resolve or reject the promise
function(resolve, reject) {
try {
// ...
if(touvabien) {
// Fulfill the promise
resolve(result);
} else {
//Error
reject(error);
} catch(error) {
reject(error);
}
});