Converts a Future into a Promise.
Converts a Future into a Promise.
Note that this conversion may not be as accurate due to the differences in Promise and Future semantics. In particular, Promises recursively flatten any object with a .then
method, and do not have a separate representation for cancellations.
Cancelled futures are converted to rejected promises with a special Cancelled()
object.
const futureToPromise = require('folktale/conversions/future-to-promise');
const Future = require('folktale/concurrency/future');
$ASSERT(
(await futureToPromise(Future.of(1))) == 1
);
try {
await futureToPromise(Future.rejected(1));
throw 'never happens';
} catch (e) {
$ASSERT(e == 1)
}
(aFuture) => {
return new Promise((resolve, reject) => {
aFuture.listen({
onResolved: (value) => resolve(value),
onRejected: (error) => reject(error),
onCancelled: () => reject(Cancelled())
});
});
}