{"id":20767011,"url":"https://github.com/binded/empty-promise","last_synced_at":"2025-05-11T08:34:03.965Z","repository":{"id":57224980,"uuid":"69512307","full_name":"binded/empty-promise","owner":"binded","description":"Constructs an \"empty promise\" that can be resolved or rejected from the \"outside\".","archived":true,"fork":false,"pushed_at":"2023-03-03T05:33:24.000Z","size":93,"stargazers_count":9,"open_issues_count":3,"forks_count":2,"subscribers_count":8,"default_branch":"master","last_synced_at":"2025-04-27T12:02:17.054Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"JavaScript","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/binded.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2016-09-28T23:35:16.000Z","updated_at":"2025-03-11T07:58:52.000Z","dependencies_parsed_at":"2024-06-19T05:29:13.137Z","dependency_job_id":"3a48c331-15b5-469d-a75d-25404470a07f","html_url":"https://github.com/binded/empty-promise","commit_stats":null,"previous_names":["blockai/empty-promise"],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/binded%2Fempty-promise","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/binded%2Fempty-promise/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/binded%2Fempty-promise/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/binded%2Fempty-promise/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/binded","download_url":"https://codeload.github.com/binded/empty-promise/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253540462,"owners_count":21924522,"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":[],"created_at":"2024-11-17T11:27:14.726Z","updated_at":"2025-05-11T08:34:03.733Z","avatar_url":"https://github.com/binded.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# empty-promise\n\n[![Build Status](https://travis-ci.org/blockai/empty-promise.svg?branch=master)](https://travis-ci.org/blockai/empty-promise)\n\nConstructs an empty promise (pun intended) that can be resolved or\nrejected from the outside.\n\nInstead of wrapping your promise in around your logic code, you may pass an empty promise through your code and resolve it later.\n\n---\nCan be quite useful for writing easier to read asynchronous tests. Also solves difficult problems in complex scenarios involving multiple paradigms of streams, events, callbacks, etc.\n\nThis style of coding shows potential for significantly simplifying promise based designs, but also shows potential to turn your code into a confusing mess. Approach it cautiously until we can hash out a few best practices.\n\nNot to be confused with `Promise.resolve()` and `Promise.reject()` from the native Promise API. These native methods only create a fully resolved or fully rejected promise on the fly. (The simplest possible empty promise usage in the first example is functionally equivalent to using `Promise.resolve()`.)\n\n## Install\n\n```bash\nnpm install --save empty-promise\n```\n\nRequires Node v6+\n\n## Basic Usage\n\n\n_The bare minimum:_ Here we create an empty promise, then resolve it with \"hello promise\" and pass the results to `console.log()`.\n\n```javascript\nimport emptyPromise from 'empty-promise'\n\nemptyPromise()\n  .resolve('hello promise')\n  .then(console.log) // logs 'hello promise'\n```\n```javascript\n// Alternatively you may use Nodejs style require\nconst emptyPromise = require('empty-promise')\n```\n\n\n_It's asynchronous:_ Just like any other promise, you can pass it around and add `then()` or `catch()` calls before it resolves.\n\n```javascript\nconst wait = emptyPromise()\n\nwait\n  .then((val) =\u003e {\n    console.log(`val`) // 'some value'\n  })\n  .catch((err) =\u003e {\n    console.err(err) // does not call because we did not reject\n  })\n\nsetTimeout(() =\u003e {\n  wait.resolve('some value')\n}, 1000)\n```\n\n\n_Works with ES6 async/await:_ Here we repeat the previous example inside an async IIFE.\n\n```javascript\nconst wait = emptyPromise()\n\n(async ()=\u003e {\n  let value\n\n  try { value = await wait }\n  catch (err) {\n    console.error(err)\n  }\n\n  console.log('${value}') // 'some value'\n})()\n\nsetTimeout(() =\u003e {\n  wait.resolve('some value')\n}, 1000)\n```\n_Only resolves once:_ Important to reiterate that an empty promise is basically still just a promise. Like any other promise, once it resolves, it will not resolve again.\n\n```javascript\nconst promise = emptyPromise()\n\n[42, 2, 17].forEach(async number =\u003e {\n  console.log(number)  // 42, 2, 17\n  const resolvedNumber = await promise.resolve(number)\n  console.log(resolvedNumber) // 42, 42, 42\n})\n```\n\n_New feature- done status:_ Now we can call `done()` method on an empty promise to find out it's resolved status. Returns `true` if resolved or rejected, and `false` if still pending. This can be used for optimizing or cancelling expensive actions that might wastefully attempt to resolve the promise a second time.\n\n```javascript\nconst promise = emptyPromise()\n\n[42, 2, 17].map(number =\u003e {\n\n  if (promise.done())\n    return promise\n\n  return promise.resolve(number)\n\n}).forEach(console.log) // 42, 42, 42\n\n```\n\nSee [Empty Promise Playground](http://github.com/skylize/empty-promise-playground) for more usage.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbinded%2Fempty-promise","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbinded%2Fempty-promise","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbinded%2Fempty-promise/lists"}