{"id":19844622,"url":"https://github.com/elazzouzihassan/javascript-promises","last_synced_at":"2025-06-13T21:05:17.444Z","repository":{"id":212516009,"uuid":"731665945","full_name":"ElazzouziHassan/JavaScript-Promises","owner":"ElazzouziHassan","description":"Welcome to the JavaScript Promises README! This document provides an overview of JavaScript Promises","archived":false,"fork":false,"pushed_at":"2023-12-14T17:10:34.000Z","size":5,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-02-28T22:50:31.798Z","etag":null,"topics":["async","await","ecmascript","es6","javascript","promises"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ElazzouziHassan.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":null,"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":"2023-12-14T15:37:08.000Z","updated_at":"2024-12-03T14:49:02.000Z","dependencies_parsed_at":null,"dependency_job_id":"1fc33113-228d-467b-a339-cbb0b26ed024","html_url":"https://github.com/ElazzouziHassan/JavaScript-Promises","commit_stats":null,"previous_names":["elazzouzihassan/javascript-promises"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/ElazzouziHassan/JavaScript-Promises","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ElazzouziHassan%2FJavaScript-Promises","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ElazzouziHassan%2FJavaScript-Promises/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ElazzouziHassan%2FJavaScript-Promises/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ElazzouziHassan%2FJavaScript-Promises/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ElazzouziHassan","download_url":"https://codeload.github.com/ElazzouziHassan/JavaScript-Promises/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ElazzouziHassan%2FJavaScript-Promises/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":259719716,"owners_count":22901238,"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":["async","await","ecmascript","es6","javascript","promises"],"created_at":"2024-11-12T13:04:58.271Z","updated_at":"2025-06-13T21:05:17.416Z","avatar_url":"https://github.com/ElazzouziHassan.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# JavaScript Promises :\n\nPromises are a feature in JavaScript that allow you to work with asynchronous code in a more organized and readable way. They represent the eventual completion or failure of an asynchronous operation and provide a cleaner alternative to using callbacks.\n\n## Creating Promises :\n\nYou can create a new Promise using the Promise constructor. It takes a function as an argument, which in turn takes two parameters: resolve and reject.\n\n```bash\nconst myPromise = new Promise((resolve, reject) =\u003e {\n  // Asynchronous operation\n  const success = true;\n\n  if (success) {\n    resolve(\"Operation successful!\");\n  } else {\n    reject(\"Operation failed!\");\n  }\n});\n```\n\n## Promise States :\n\nA Promise can be in one of three states:\n\n- **Pending**: The initial state, neither fulfilled nor rejected.\n- **Fulfilled**: The operation completed successfully.\n- **Rejected**: The operation failed.\n\n## Handling Promises :\n\nYou can handle the results of a Promise using the then and catch methods.\n\n```bash\nmyPromise\n  .then((result) =\u003e {\n    console.log(result); // Operation successful!\n  })\n  .catch((error) =\u003e {\n    console.error(error); // Operation failed!\n  });\n```\n\n## Chaining Promises :\n\nPromises can be chained together, making it easier to handle multiple asynchronous operations sequentially.\n\n```bash\nconst promise1 = new Promise((resolve) =\u003e {\n  resolve(\"First operation complete\");\n});\n\nconst promise2 = (message) =\u003e {\n  return new Promise((resolve) =\u003e {\n    resolve(`${message}, Second operation complete`);\n  });\n};\n\npromise1\n  .then((result) =\u003e {\n    return promise2(result);\n  })\n  .then((result) =\u003e {\n    console.log(result);\n  });\n```\n\n## Example: Fetching Data :\n\nA common use case for Promises is handling asynchronous operations like fetching data from an API.\n\n```bash\nfunction fetchData(url) {\n  return new Promise((resolve, reject) =\u003e {\n    fetch(url)\n      .then((res) =\u003e {\n        if (!res.ok) {\n          throw new Error(`HTTP error! Status: ${res.status}`);\n        }\n        return res.json();\n      })\n      .then((data) =\u003e {\n        resolve(data);\n      })\n      .catch((error) =\u003e {\n        reject(error);\n      });\n  });\n}\n\n// Example usage\nfetchData(\"https://jsonplaceholder.typicode.com/todos/1\")\n  .then((data) =\u003e {\n    console.log(data);\n  })\n  .catch((error) =\u003e {\n    console.error(error);\n  });\n```\n\n---\n\n-by @Elazzouzi Hassan (Wizardy - 🧙‍♂️)","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Felazzouzihassan%2Fjavascript-promises","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Felazzouzihassan%2Fjavascript-promises","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Felazzouzihassan%2Fjavascript-promises/lists"}