promiseToFuture

Converts a Promise to a folktale Future.

Signature

promiseToFuture(aPromise)
forall e, v:
  (Promise v e) => Future e v

Documentation

Converts a Promise to a folktale Future.

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.

If a Promise contains a rejection with Folktale's special Cancelled() value, then the resulting Future will be a cancelled Future rather than a rejected one.

Example:

const promiseToFuture = require('folktale/conversions/promise-to-future');

promiseToFuture(Promise.resolve(1));
// => Future.resolve(1)

promiseToFuture(Promise.reject(1));
// => Future.reject(1)

Properties

Source Code

Defined in source/conversions/promise-to-future.js at line 19, column 0
(aPromise) => {
  const deferred = new Deferred();
  aPromise.then(
    (value) => deferred.resolve(value),
    (error) => {
      if (Cancelled.hasInstance(error)) {
        deferred.cancel();
      } else {
        deferred.reject(error);
      }
    }
  );
  return deferred.future();
}
Stability
stable
Licence
MIT
Module
folktale/conversions/promise-to-future
Authors
Copyright
(c) 2013-2017 Quildreen Motta, and CONTRIBUTORS
Authors
  • Quildreen Motta
Maintainers
  • Quildreen Motta <queen@robotlolita.me> (http://robotlolita.me/)