{"id":19136938,"url":"https://github.com/singcl/promise","last_synced_at":"2026-06-17T08:32:20.521Z","repository":{"id":57160433,"uuid":"125081863","full_name":"singcl/promise","owner":"singcl","description":"🦁 Step by step build a Promise Class \u0026 Promise/Promise A+","archived":false,"fork":false,"pushed_at":"2018-05-16T08:15:13.000Z","size":58,"stargazers_count":1,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-11-13T08:14:06.931Z","etag":null,"topics":["callback","coverage","david","fossa-status","promise","size","then","thenable","travis-ci"],"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/singcl.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","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":"2018-03-13T16:28:58.000Z","updated_at":"2025-09-06T09:15:04.000Z","dependencies_parsed_at":"2022-09-09T07:31:04.813Z","dependency_job_id":null,"html_url":"https://github.com/singcl/promise","commit_stats":null,"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"purl":"pkg:github/singcl/promise","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/singcl%2Fpromise","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/singcl%2Fpromise/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/singcl%2Fpromise/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/singcl%2Fpromise/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/singcl","download_url":"https://codeload.github.com/singcl/promise/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/singcl%2Fpromise/sbom","scorecard":{"id":827022,"data":{"date":"2025-08-11","repo":{"name":"github.com/singcl/promise","commit":"21801b95713dc14914f4940ba862ab9d32528770"},"scorecard":{"version":"v5.2.1-40-gf6ed084d","commit":"f6ed084d17c9236477efd66e5b258b9d4cc7b389"},"score":3,"checks":[{"name":"Packaging","score":-1,"reason":"packaging workflow not detected","details":["Warn: no GitHub/GitLab publishing workflow detected."],"documentation":{"short":"Determines if the project is published as a package that others can easily download, install, easily update, and uninstall.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#packaging"}},{"name":"Binary-Artifacts","score":10,"reason":"no binaries found in the repo","details":null,"documentation":{"short":"Determines if the project has generated executable (binary) artifacts in the source repository.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#binary-artifacts"}},{"name":"Pinned-Dependencies","score":-1,"reason":"no dependencies found","details":null,"documentation":{"short":"Determines if the project has declared and pinned the dependencies of its build process.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#pinned-dependencies"}},{"name":"Token-Permissions","score":-1,"reason":"No tokens found","details":null,"documentation":{"short":"Determines if the project's workflows follow the principle of least privilege.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#token-permissions"}},{"name":"SAST","score":0,"reason":"no SAST tool detected","details":["Warn: no pull requests merged into dev branch"],"documentation":{"short":"Determines if the project uses static code analysis.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#sast"}},{"name":"Dangerous-Workflow","score":-1,"reason":"no workflows found","details":null,"documentation":{"short":"Determines if the project's GitHub Action workflows avoid dangerous patterns.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#dangerous-workflow"}},{"name":"Maintained","score":0,"reason":"0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0","details":null,"documentation":{"short":"Determines if the project is \"actively maintained\".","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#maintained"}},{"name":"Code-Review","score":0,"reason":"Found 0/30 approved changesets -- score normalized to 0","details":null,"documentation":{"short":"Determines if the project requires human code review before pull requests (aka merge requests) are merged.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#code-review"}},{"name":"CII-Best-Practices","score":0,"reason":"no effort to earn an OpenSSF best practices badge detected","details":null,"documentation":{"short":"Determines if the project has an OpenSSF (formerly CII) Best Practices Badge.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#cii-best-practices"}},{"name":"Security-Policy","score":0,"reason":"security policy file not detected","details":["Warn: no security policy file detected","Warn: no security file to analyze","Warn: no security file to analyze","Warn: no security file to analyze"],"documentation":{"short":"Determines if the project has published a security policy.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#security-policy"}},{"name":"Vulnerabilities","score":10,"reason":"0 existing vulnerabilities detected","details":null,"documentation":{"short":"Determines if the project has open, known unfixed vulnerabilities.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#vulnerabilities"}},{"name":"Fuzzing","score":0,"reason":"project is not fuzzed","details":["Warn: no fuzzer integrations found"],"documentation":{"short":"Determines if the project uses fuzzing.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#fuzzing"}},{"name":"Signed-Releases","score":-1,"reason":"no releases found","details":null,"documentation":{"short":"Determines if the project cryptographically signs release artifacts.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#signed-releases"}},{"name":"License","score":10,"reason":"license file detected","details":["Info: project has a license file: LICENSE:0","Info: FSF or OSI recognized license: MIT License: LICENSE:0"],"documentation":{"short":"Determines if the project has defined a license.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#license"}},{"name":"Branch-Protection","score":0,"reason":"branch protection not enabled on development/release branches","details":["Warn: branch protection not enabled for branch 'master'"],"documentation":{"short":"Determines if the default and release branches are protected with GitHub's branch protection settings.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#branch-protection"}}]},"last_synced_at":"2025-08-23T16:48:47.644Z","repository_id":57160433,"created_at":"2025-08-23T16:48:47.645Z","updated_at":"2025-08-23T16:48:47.645Z"},"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":34441284,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-26T15:22:16.424Z","status":"online","status_checked_at":"2026-06-17T02:00:05.408Z","response_time":127,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":["callback","coverage","david","fossa-status","promise","size","then","thenable","travis-ci"],"created_at":"2024-11-09T06:35:55.796Z","updated_at":"2026-06-17T08:32:20.502Z","avatar_url":"https://github.com/singcl.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"![promise](./img/promise.png)\n## Promise/A+\n\n[![Travis](https://img.shields.io/travis/singcl/promise.svg?style=flat-square)](https://www.travis-ci.org/singcl/promise)\n[![npm (scoped)](https://img.shields.io/npm/v/@singcl/promise.svg?style=flat-square)](https://www.npmjs.com/package/@singcl/promise)\n[![code style: prettier](https://img.shields.io/badge/code_style-prettier-10de6e.svg?style=flat-square)](https://github.com/prettier/prettier)\n![David](https://img.shields.io/david/dev/singcl/promise.svg?style=flat-square)\n![David](https://img.shields.io/david/singcl/promise.svg?style=flat-square)\n![Github file size](https://img.shields.io/github/size/singcl/promise/dist/index.js.svg?style=flat-square)\n![npm](https://img.shields.io/npm/dm/@singcl/promise.svg?style=flat-square)\n\n*badge: https://img.shields.io/*\n\n### USAGE\n\n1. install: `npm i @singcl/promise`\n2. In project:\n```js\nvar Promise = require('@singcl/promise')\n\nvar promise = new Promise(function(resolve, reject) {\n\tresolve('This is an example')\n})\n\npromise.then(function(v) {\n\tconsole.log(v)\n})\n\n// This is an example\n```\n\n\n### Promise标准简读\n#### 1. 只有一个then方法，没有race，all等方法，甚至没有构造函数\nPromise标准中仅指定了Promise对象的then方法的行为，其它一切我们常见的方法/函数都并没有指定，包括catch，race，all等常用方法，甚至也没有指定该如何构造出一个Promise对象。另外then也没有一般实现中（Q, $q等）所支持的第三个参数，一般称onProgress.\n\n#### 2. then方法返回一个新的Promise\nPromise的then方法返回一个新的Promise，而不是返回this\n```js\n// 假设已经实现了一个Promise对象promise1\nvar promise2 = promise1.then(/*your code*/)\nconsole.log(promise2 === promise1)  // fales\n```\n#### 3. 不同Promise的实现需要可以相互调用\n#### 4. 三个状态：pending,resolved,rejected\nPromise的初始状态为pending，它可以由此状态转换为fulfilled（resolved）或者rejected,Promise的状态一旦从pending变为fulfilled或者rejected就**永远**不会在变化.\n#### 5. 更多规范参照[Promise官方英文文档](https://promisesaplus.com/)\n\n### **Promise实现**\n\n- [x] Promise构造函数基本结构\n- [x] Promise原型方法:then\n- [x] Promise原型方法:catch\n\n### 问题探讨\nthen方法的callbakc为什么要设计成异步调用？\n\n答：Promise 的机制就是 then 回调函数必须异步执行。为什么？因为这样保障了代码执行顺序的一致性。\n\n先看一个场景：\n```js\npromise.then(function() { \n  if (trueOrFalse) { \n    // 同步执行 \n    foo(); \n  } else { \n    // 异步执行 (如：使用第三方库)\n    setTimeout(function() { \n        foo(); \n    }) \n  } \n}); \n\nbar();\n```\n如果 promise then 回调是同步执行的，请问 foo() 和 bar() 函数谁先执行?\n\n答案是，如果 trueOrFalse 为 true 则 foo() 先执行，bar() 后执行；否则 bar() 先执行，foo() 后执行。在大部分情况下，你没法预料到 trueOrFalse 的值，这也就意味着，你不能确定这段代码真正的执行顺序，这可能会导致一些难以想到的 bug。\n\n如果 promise then 回调是异步执行的，请问 foo() 和 bar() 函数谁先执行?\n答案一目了然，bar() 先执行，foo() 后执行。\n\n所以为了保证代码执行顺序的一致性， then 回调必须保证是异步的。\n\n除此之外，Promise 的设计还是为了解决以下情形导致的不确定性：\n- Call the callback too early\n- Call the callback too late (or never)\n- Call the callback too few or too many times\n- Fail to pass along any necessary environment/parameters\n- Swallow any errors/exceptions that may happen\n\n更多关于该问题的探讨可以阅读：https://www.zhihu.com/question/57071244\n## License [![FOSSA Status](https://app.fossa.io/api/projects/git%2Bgithub.com%2Fsingcl%2Fpromise.svg?type=shield)](https://app.fossa.io/projects/git%2Bgithub.com%2Fsingcl%2Fpromise?ref=badge_shield)\n\n[![FOSSA Status](https://app.fossa.io/api/projects/git%2Bgithub.com%2Fsingcl%2Fpromise.svg?type=large)](https://app.fossa.io/projects/git%2Bgithub.com%2Fsingcl%2Fpromise?ref=badge_large)","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsingcl%2Fpromise","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsingcl%2Fpromise","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsingcl%2Fpromise/lists"}