{"id":19136938,"url":"https://github.com/singcl/promise","last_synced_at":"2025-11-13T08:03:39.158Z","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":2,"open_issues_count":0,"forks_count":1,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-02-03T19:40:11.709Z","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":"2023-07-18T07:44:02.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,"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","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":240215473,"owners_count":19766423,"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":["callback","coverage","david","fossa-status","promise","size","then","thenable","travis-ci"],"created_at":"2024-11-09T06:35:55.796Z","updated_at":"2025-11-13T08:03:34.120Z","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"}