All Projects → Thimoteus → purescript-promises

Thimoteus / purescript-promises

Licence: MIT License
An alternative effect monad for PureScript.

Programming Languages

purescript
368 projects
javascript
184084 projects - #8 most used programming language

Labels

Projects that are alternatives of or similar to purescript-promises

organiser
An organic web framework for organized web servers.
Stars: ✭ 58 (+152.17%)
Mutual labels:  promise
promise
Common interface for simple asynchronous placeholders.
Stars: ✭ 66 (+186.96%)
Mutual labels:  promise
debounce-async
A debounce function that delays invoking asynchronous functions.
Stars: ✭ 21 (-8.7%)
Mutual labels:  promise
freenom
🌐 Feenom Promise/Async/Await
Stars: ✭ 19 (-17.39%)
Mutual labels:  promise
nice-grpc
A TypeScript gRPC library that is nice to you
Stars: ✭ 120 (+421.74%)
Mutual labels:  promise
Eventually
A Swift Future/Promise library that can be used to model and transform asynchronous results
Stars: ✭ 19 (-17.39%)
Mutual labels:  promise
reactphp-child-process-promise
No description or website provided.
Stars: ✭ 12 (-47.83%)
Mutual labels:  promise
Actuate
One line easy actuation of CSS animation sequences
Stars: ✭ 42 (+82.61%)
Mutual labels:  promise
Promise
Asynchronous Programming with Promises
Stars: ✭ 15 (-34.78%)
Mutual labels:  promise
popyt
A very easy to use Youtube Data v3 API wrapper.
Stars: ✭ 42 (+82.61%)
Mutual labels:  promise
combine-promises
Like Promise.all(array) but with an object instead of an array.
Stars: ✭ 181 (+686.96%)
Mutual labels:  promise
sfdx-lightning-api-component
⚡️ Promise-based service component for calling REST API from Lightning Aura Components without Named Credentials.
Stars: ✭ 62 (+169.57%)
Mutual labels:  promise
promise
A step by step implementation practice of Promise class
Stars: ✭ 31 (+34.78%)
Mutual labels:  promise
wise-river
Object streaming the way it should be.
Stars: ✭ 33 (+43.48%)
Mutual labels:  promise
redis-memolock
Redis MemoLock - Distributed Caching with Promises
Stars: ✭ 63 (+173.91%)
Mutual labels:  promise
easy-promise-queue
An easy JavaScript Promise queue which is automatically executed, concurrency controlled and suspendable.
Stars: ✭ 31 (+34.78%)
Mutual labels:  promise
event-worker
A simpler way of dealing with Web Workers
Stars: ✭ 18 (-21.74%)
Mutual labels:  promise
lifx-http-api
💡 Thin wrapper around the Lifx HTTP API (http://api.developer.lifx.com/)
Stars: ✭ 17 (-26.09%)
Mutual labels:  promise
arraync
Async Array methods polyfills
Stars: ✭ 16 (-30.43%)
Mutual labels:  promise
swear
🙏 Flexible promise handling with Javascript
Stars: ✭ 56 (+143.48%)
Mutual labels:  promise

purescript-promises

Build Status

An alternative effect monad for PureScript.

Use this for easy interop with existing promise-based JavaScript libraries.

Usage

With monadic do notation, or with promise-chaining notation. The following are equivalent:

myApply :: forall a b. Promise (a -> b) -> Promise a -> Promise b
myApply pab pa = do
  ab <- pab
  a <- pa
  pure (ab a)

myApplyChained :: forall a b. Promise (a -> b) -> Promise a -> Promise b
myApplyChained pab pa = pab # then' \ ab -> pa # then' \ a -> resolve (ab a)

in fact, if you squint a little, myApplyChained looks like the following JavaScript:

var myApplyChained = function (pab, pa) {
  pab.then(function (ab) {
    pa.then(function (a) {
      return Promise.resolve(ab(a));
    });
  });
}

Also see the tests folder.

eagerness

While promises are eager, this library provides the Deferred typeclass to ensure promises don't prematurely run their side-effects until safely consumed with runPromise, or the nonstandard done.

In fact, not only are promises eager, but they're eager about being eager. They really want to run:

delay example

promDelay :: Deferred => Promise Unit
promDelay = do
  p1
  p2
  p3
  where
    p1 = do
      Console.log "one"
      Promise.delay (Milliseconds 1000.0) unit
      Console.log "two"
    p2 = do
      Promise.delay (Milliseconds 1000.0) unit
      Console.log "three"
    p3 = do
      Promise.delay (Milliseconds 1000.0) unit
      Console.log "four"

this will output one, wait one second, then four three two. In order to obtain the desired behavior of waiting one second between each log, it's necessary to add type annotations to p1, p2 and p3:

p1 :: Deferred => Promise Unit
p1 = do ...

parallel (<*>)

promApply :: Deferred => Promise Unit
promApply = p1 *> p2 *> p3
  where
    p1 :: Deferred => Promise Unit
    p1 = do
      Console.log "<*> is"
      Promise.delay (Milliseconds 1000.0) unit
      Console.log "done"
    p2 :: Deferred => Promise Unit
    p2 = do
      Promise.delay (Milliseconds 3000.0) unit
      Console.log "parallel"
    p3 :: Deferred => Promise Unit
    p3 = do
      Promise.delay (Milliseconds 2000.0) unit
      Console.log "in"

Note that difference (between this example and the last) that we're using (*>) instead of implicit (>>=)s. And even though we added the Deferred constraint, it will still take 3 seconds to run total -- not 6, as it would be using do notation.

FFI example

exports.myPromise = new Promise(function (resolve, reject) {
  resolve(5);
});
foreign import myPromise :: Promise Int

doSomething :: Deferred => Promise Unit
doSomething = do
  p <- myPromise
  Console.logShow p

main :: Effect Unit  
main
  = runPromise
    (const (log "success callback"))
    (const (error "error callback"))
    doSomething

Installation

bower install --save purescript-promises

See also

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].