{"id":27362609,"url":"https://github.com/thimoteus/purescript-promises","last_synced_at":"2025-04-13T03:26:14.494Z","repository":{"id":58225466,"uuid":"101372826","full_name":"Thimoteus/purescript-promises","owner":"Thimoteus","description":"An alternative effect monad for PureScript.","archived":false,"fork":false,"pushed_at":"2019-05-08T18:09:22.000Z","size":64,"stargazers_count":23,"open_issues_count":2,"forks_count":5,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-03-12T09:43:40.532Z","etag":null,"topics":["javascript","promise","purescript"],"latest_commit_sha":null,"homepage":null,"language":"PureScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Thimoteus.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-08-25T06:23:42.000Z","updated_at":"2022-01-14T05:02:52.000Z","dependencies_parsed_at":"2022-08-31T03:24:33.288Z","dependency_job_id":null,"html_url":"https://github.com/Thimoteus/purescript-promises","commit_stats":null,"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Thimoteus%2Fpurescript-promises","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Thimoteus%2Fpurescript-promises/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Thimoteus%2Fpurescript-promises/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Thimoteus%2Fpurescript-promises/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Thimoteus","download_url":"https://codeload.github.com/Thimoteus/purescript-promises/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248550624,"owners_count":21122932,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["javascript","promise","purescript"],"created_at":"2025-04-13T03:26:13.696Z","updated_at":"2025-04-13T03:26:14.488Z","avatar_url":"https://github.com/Thimoteus.png","language":"PureScript","readme":"# purescript-promises\n[![Build Status](https://travis-ci.org/Thimoteus/purescript-promises.svg?branch=master)](https://travis-ci.org/Thimoteus/purescript-promises)\n\nAn alternative effect monad for PureScript.\n\nUse this for easy interop with existing promise-based JavaScript libraries.\n\n## Usage\n\nWith monadic `do` notation, or with promise-chaining notation. The following are\nequivalent:\n\n```purescript\nmyApply :: forall a b. Promise (a -\u003e b) -\u003e Promise a -\u003e Promise b\nmyApply pab pa = do\n  ab \u003c- pab\n  a \u003c- pa\n  pure (ab a)\n\nmyApplyChained :: forall a b. Promise (a -\u003e b) -\u003e Promise a -\u003e Promise b\nmyApplyChained pab pa = pab # then' \\ ab -\u003e pa # then' \\ a -\u003e resolve (ab a)\n```\n\nin fact, if you squint a little, `myApplyChained` looks like the following JavaScript:\n\n```javascript\nvar myApplyChained = function (pab, pa) {\n  pab.then(function (ab) {\n    pa.then(function (a) {\n      return Promise.resolve(ab(a));\n    });\n  });\n}\n```\n\nAlso see the `tests` folder.\n\n### eagerness\n\nWhile promises are [eager](https://medium.com/@avaq/broken-promises-2ae92780f33),\nthis library provides the `Deferred` typeclass to ensure promises don't prematurely\nrun their side-effects until safely consumed with `runPromise`, or the nonstandard\n`done`.\n\nIn fact, not only are promises eager, but they're eager about being eager. They *really*\nwant to run:\n\n### delay example\n\n```purescript\npromDelay :: Deferred =\u003e Promise Unit\npromDelay = do\n  p1\n  p2\n  p3\n  where\n    p1 = do\n      Console.log \"one\"\n      Promise.delay (Milliseconds 1000.0) unit\n      Console.log \"two\"\n    p2 = do\n      Promise.delay (Milliseconds 1000.0) unit\n      Console.log \"three\"\n    p3 = do\n      Promise.delay (Milliseconds 1000.0) unit\n      Console.log \"four\"\n```\n\nthis will output `one`, wait one second, then `four` `three` `two`.\nIn order to obtain the desired behavior of waiting one second between\n*each* log, it's necessary to add type annotations to `p1`, `p2` and\n`p3`:\n\n```purescript\np1 :: Deferred =\u003e Promise Unit\np1 = do ...\n```\n\n### parallel `(\u003c*\u003e)`\n\n```purescript\npromApply :: Deferred =\u003e Promise Unit\npromApply = p1 *\u003e p2 *\u003e p3\n  where\n    p1 :: Deferred =\u003e Promise Unit\n    p1 = do\n      Console.log \"\u003c*\u003e is\"\n      Promise.delay (Milliseconds 1000.0) unit\n      Console.log \"done\"\n    p2 :: Deferred =\u003e Promise Unit\n    p2 = do\n      Promise.delay (Milliseconds 3000.0) unit\n      Console.log \"parallel\"\n    p3 :: Deferred =\u003e Promise Unit\n    p3 = do\n      Promise.delay (Milliseconds 2000.0) unit\n      Console.log \"in\"\n```\n\nNote that difference (between this example and the last) that we're using `(*\u003e)`\ninstead of implicit `(\u003e\u003e=)`s. And even though we added the `Deferred` constraint,\nit will still take 3 seconds to run total -- not 6, as it would be using do notation.\n\n### FFI example\n```javascript\nexports.myPromise = new Promise(function (resolve, reject) {\n  resolve(5);\n});\n```\n\n```purescript\nforeign import myPromise :: Promise Int\n\ndoSomething :: Deferred =\u003e Promise Unit\ndoSomething = do\n  p \u003c- myPromise\n  Console.logShow p\n\nmain :: Effect Unit  \nmain\n  = runPromise\n    (const (log \"success callback\"))\n    (const (error \"error callback\"))\n    doSomething\n```\n\n## Installation\n\n`bower install --save purescript-promises`\n\n## See also\n* [purescript-aff-promise](https://github.com/nwolverson/purescript-aff-promise)\n* [purescript-aff](https://github.com/slamdata/purescript-aff)\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthimoteus%2Fpurescript-promises","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fthimoteus%2Fpurescript-promises","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthimoteus%2Fpurescript-promises/lists"}