I don't pretend that impossible to do with current promises.
I'm just saying that we can get a complexity that could be easily avoided.
And a catch/then doesn't allow to return in the first promise onFulfilled,
without embedded promises.
I think a practical example might better illustrate the thing.
Imagine, we have to check a data integrity, firstly internal, or from
external sources, if the internal data is corrupted.
The integrity controller is a foreign promisable controller , with a custom
hash method & a fingerprint... we can't touch it.
```
var foreignController;
foreignController = (function () {
var fingerPrint,
hash,
controller;
fingerPrint = 'aforeignfingerprint';
hash = function hash(data) {
// foreign ownmade hash calculator
};
controller = function controller(resolve, reject, negociate) {
var data,
isValid,
sources;
data = this.data;
isValid = fingerPrint === hash(data);
if (isValid) {
return resolve(data);
}
sources = this.sources;
if (sources.length) {
// not an error, we only remove that source
// and try with the next one
return negociate(sources.shift());
}
reject(new Error('No valid data found'));
};
return controller;
}());
```
Now, the data ownmade data checker :
```
var getDataSync,
onFulFilled,
onRejected,
onNegociated,
handler,
controller;
getDataSync function getDataSync(source) {
// data collector
};
onFulFilled = function onFulFilled(value) {
// valid data treatment
};
onRejected = function onRejected(reason) {
// no valid data found
};
onNegociated = function onNegociated(resolve, reject, negociate, retry,
value) {
var source;
source = this.sources[0];
this.data = getDataSync(source);
console.log('data source corrupted : ' + value);
retry();
};
handler = {
data: 'some data',
sources: ['internal', 'an.external.url', 'another.external.url', '...']
};
controller = foreignController.bind(handler);
new Promise(controller)
.then(onFulFilled, onRejected, onNegociated.bind(handler))
/* a lot of thens */;
```
Personnaly, I don't see how the current promises can solve it as simply.
Michaël Rouges - https://github.com/Lcfvs - @Lcfvs
_______________________________________________
es-discuss mailing list
[email protected]
https://mail.mozilla.org/listinfo/es-discuss