{"id":16052435,"url":"https://github.com/keillion/mutablepromise","last_synced_at":"2026-02-09T02:32:42.276Z","repository":{"id":65522177,"uuid":"470485635","full_name":"Keillion/MutablePromise","owner":"Keillion","description":"Wrapper for Promise.  Resolvable, rejectable, redirectable. ","archived":false,"fork":false,"pushed_at":"2025-01-02T11:44:38.000Z","size":23,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-16T17:11:40.260Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"unlicense","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Keillion.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":"2022-03-16T08:01:51.000Z","updated_at":"2025-01-02T11:44:42.000Z","dependencies_parsed_at":"2024-10-27T17:20:04.744Z","dependency_job_id":"ec6c1a30-999a-4d9a-9ad4-dc6186a4125c","html_url":"https://github.com/Keillion/MutablePromise","commit_stats":{"total_commits":17,"total_committers":1,"mean_commits":17.0,"dds":0.0,"last_synced_commit":"70ea0b81140f1aa5fb3d7f714b6f40d06ded5410"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Keillion%2FMutablePromise","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Keillion%2FMutablePromise/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Keillion%2FMutablePromise/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Keillion%2FMutablePromise/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Keillion","download_url":"https://codeload.github.com/Keillion/MutablePromise/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244156658,"owners_count":20407545,"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-10-09T01:08:56.573Z","updated_at":"2026-02-09T02:32:42.195Z","avatar_url":"https://github.com/Keillion.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# MutablePromise\n\nWrapper for Promise. Resolvable, rejectable, redirectable. \n\n\u003cbr\u003e\n\n## Import\n\n\u003cbr\u003e\n\ninstall\n```\nnpm i mutable-promise -S\n```\n```\nyarn add mutable-promise\n```\n\n\u003cbr\u003e\n\nimport\n```mjs\nimport MutablePromise from 'mutable-promise';\n```\n```js\nconst MutablePromise = require('mutable-promise');\n```\n\n\u003cbr\u003e\n\nCDN\n```html\n\u003cscript type=\"module\"\u003e\n    import MutablePromise from 'https://cdn.jsdelivr.net/npm/mutable-promise@1.1.15/dist/index.esm.min.js';\n\u003c/script\u003e\n```\n```html\n\u003cscript src=\"https://cdn.jsdelivr.net/npm/mutable-promise@1.1.15/dist/index.min.js\"\u003e\u003c/script\u003e\n```\n\n## Example\n\n\u003cbr\u003e\n\n`resolve` outside of the executor.\nGet `status` of the `promise`.\n```js\n// can use like a native `Promise`\nlet p = new MutablePromise((_, rj)=\u003esetTimeout(rj,100)); // try to reject after 100ms\n\n// can get status\nconsole.log(p.status); // \"pending\"\nconsole.log(p.isPending); // true\n\n// can resolve outside\np.then(anything=\u003econsole.log(anything)); // 'resolve called' \np.resolve('resolve called'); // resolve immediately\nconsole.log(p.status); // \"fulfilled\"\nconsole.log(p.isFulfilled); // true\n\n(async()=\u003e{\n    await new Promise(rs=\u003esetTimeout(rs,200)); // wait 200ms\n\n    // status will not change after `fulfilled` or `rejected`\n    console.log(p.status); // \"fulfilled\"\n    console.log(p.isFulfilled); // true\n})();\n```\n\n\u003cbr\u003e\n\n`reject` outside of the executor.\n```js\nlet p = new MutablePromise(rs=\u003esetTimeout(rs,100)); // try to resolve after 100ms\n\n// can reject outside\np.catch(anything=\u003econsole.log(anything)); // 'reject called'\np.reject('reject called'); // reject immediately\nconsole.log(p.status); // \"rejected\"\nconsole.log(p.isRejected); // true\n```\n\n\u003cbr\u003e\n\nAllow setting a `promise` as argument.\n```js\n// simple promise\nnew MutablePromise(Promise.resolve('1'));\n// typical promise\nlet nativeP = new Promise(rs=\u003esetTimeout(rs,200));\nnew MutablePromise(nativeP);\n// wrapper a fetch\nnew MutablePromise(fetch('./'));\n// nested MutablePromise\nlet p = new MutablePromise(rs=\u003esetTimeout(rs,200));\nnew MutablePromise(p);\n// promise like\nlet pLike = { then: function(){ return 'a'; } };\nnew MutablePromise(pLike);\n```\n\n\u003cbr\u003e\n\nAllow setting no argument or `null`. Then define a `task` later.\n```js\nlet p = new MutablePromise(); // or `new MutablePromise(null)`\np.then(anything=\u003econsole.log(anything)); // 'msg from later define task'\n// The property `task` can accept the same parameter type as the constructor of `MutablePromise`\np.task = rs=\u003ers('msg from later define task');\n```\n\n\u003cbr\u003e\n\nCan change `task` before `fulfilled` or `rejected`.\n```js\nlet p = new MutablePromise(rs=\u003esetTimeout(()=\u003e{rs('original task')},100));\np.then(anything=\u003econsole.log(anything)); // 'new task'\np.task = new MutablePromise(rs=\u003esetTimeout(()=\u003e{rs('new task')},200));\n```\n\n\u003cbr\u003e\n\nA change make no sense after `fulfilled` or `rejected`.\n```js\nlet p = new MutablePromise(Promise.resolve('resolved'));\np.then(anything=\u003econsole.log(anything)); // 'resolved'\n\n(async()=\u003e{\n    // wait next js event loop\n    await new Promise(rs=\u003esetTimeout(rs,0));\n\n    console.log(p.status); // \"fulfilled\"\n    p.catch(anything=\u003econsole.log(anything)); // will not log anything\n    p.task = Promise.reject('make no sense');\n    \n    await new Promise(rs=\u003esetTimeout(rs,0));\n    p.task = (rs)=\u003e{\n        console.log('function still run after `fulfilled` or `rejected`');\n        rs('but will not resolve or reject');\n    };\n})();\n```\n\n\u003cbr\u003e\n\nSet `task` as `null` can cancel the orignial task.\n```js\nlet p = new MutablePromise(resolve=\u003esetTimeout(()=\u003e{\n    console.log('the executor will run anyway');\n    resolve('original task');\n},100));\np.then(anything=\u003econsole.log(anything)); // will not log anything\np.task = null;\n// the promise will keep `pending`\nsetTimeout(()=\u003e{\n    console.log(p.status);\n},200)\n// you can define a new `task` later\n```\n\n\u003cbr\u003e\n\nIf a `MutablePromise` has been `fulfilled` or `rejected`, you can define a new `MutablePromise` instead.\n```js\n(async()=\u003e{\n    let p = new MutablePromise(Promise.resolve());\n    await new Promise(rs=\u003esetTimeout(rs,0)); // wait next js event loop\n    console.log(p.status); // \"fulfilled\"\n    \n    p = new MutablePromise(resolve=\u003esetTimeout(()=\u003e{resolve('you can define a new `MutablePromise` instead')},100));\n    p.then(anything=\u003econsole.log(anything));\n})();\n```\n\n## TODO\n\nMaybe need to wrap other promise function like `all`, `resolve`, `race` and so on, to `MutablePromise` edition.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkeillion%2Fmutablepromise","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkeillion%2Fmutablepromise","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkeillion%2Fmutablepromise/lists"}