futureToPromise

Converts a Future into a Promise.

Signature

futureToPromise(aFuture)
forall e, v:
  (Future e v) => Promise v e

Documentation

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.

Example:

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)
}

Properties

Source Code

Defined in source/conversions/future-to-promise.js at line 19, column 0
(aFuture) => {
  return new Promise((resolve, reject) => {
    aFuture.listen({
      onResolved: (value) => resolve(value),
      onRejected: (error) => reject(error),
      onCancelled: ()     => reject(Cancelled())
    });
  });
}
Stability
stable
Licence
MIT
Module
folktale/conversions/future-to-promise
Authors
Copyright
(c) 2013-2017 Quildreen Motta, and CONTRIBUTORS
Authors
  • Quildreen Motta
Maintainers
  • Quildreen Motta <queen@robotlolita.me> (http://robotlolita.me/)