{"id":20314365,"url":"https://github.com/normandy72/asynchronous-behavior","last_synced_at":"2026-05-11T14:34:08.495Z","repository":{"id":153902732,"uuid":"586828344","full_name":"Normandy72/Asynchronous-Behavior","owner":"Normandy72","description":"Asynchronous Behavior with Promises and $q in AngularJS.  Coursera course \"Single Page Web Applications with AngularJS\" by Yaakov Chaikin.","archived":false,"fork":false,"pushed_at":"2023-01-09T12:31:36.000Z","size":68,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-04T08:44:57.017Z","etag":null,"topics":["angular","angularjs","html","html5","javascript","js"],"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/Normandy72.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-01-09T10:27:09.000Z","updated_at":"2023-01-09T12:31:41.000Z","dependencies_parsed_at":null,"dependency_job_id":"2a6267c1-d560-49b3-8ca3-566167ed5c5d","html_url":"https://github.com/Normandy72/Asynchronous-Behavior","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/Normandy72/Asynchronous-Behavior","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Normandy72%2FAsynchronous-Behavior","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Normandy72%2FAsynchronous-Behavior/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Normandy72%2FAsynchronous-Behavior/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Normandy72%2FAsynchronous-Behavior/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Normandy72","download_url":"https://codeload.github.com/Normandy72/Asynchronous-Behavior/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Normandy72%2FAsynchronous-Behavior/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":32899146,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-10T13:40:02.631Z","status":"online","status_checked_at":"2026-05-11T02:00:05.975Z","response_time":120,"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":["angular","angularjs","html","html5","javascript","js"],"created_at":"2024-11-14T18:14:58.406Z","updated_at":"2026-05-11T14:34:08.478Z","avatar_url":"https://github.com/Normandy72.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"### Before Promises - Callbacks\n```\nasyncFunction(function(){\n    // do when asyncFunction is done\n});\n```\nNo easy and straighforward way to pass the results of asyncFunction back to its caller, especially, if the real recipient of the result ia s few layers away.\n\nIn addition, what would happen if you needed to chain a few asynchronous pieces together? For example, when asyncFunction1 would complete, instead of returning the value, you would still have to call asyncFunction2. When that would complete, you would finally move asyncFunction3, and only then deal with all the results combined together.\n\n```\nasyncFunction1(function(){\n    asyncFunction2(function(){\n        asyncFunction(function(){\n            // do when asyncFunction is done\n        });\n    });\n});\n```\nIt's very hard to read this code, and that's not even showing the error handling. \n\n\n### AngularJS and New ES6 API\n#### Promise\nObject which can be passed around or returned that holds references to the outcome of asynchronous behavior.\n\nIn Angular, promises are created through the the `$q` service.\n#### AngularJS $q Service Promise\n```\nfunction asyncFunction(){\n    var deferred = $q.defer();\n    if(...){ deferred.resolve(result); }\n    else{deferred.reject(error); }\n    return deferrer.promise;\n}\n```\n* `var deferred = $q.defer();` - creates async environment with all the hooks into it, including the promise object.\n* `deferred.resolve(result);` - marks _successful_ completion, wraps data for the promise.\n* `deferred.reject(error);` - marks _unseccessful_ completion, wraps data for the promise.\n* `return deferrer.promise;` - returns promise to caller (a hook back to this entire process)\n\nThis code can be done asynchronously: \n```\n...\n    if(...){ deferred.resolve(result); }\n    else{deferred.reject(error); }\n...\n```\nThe next step is for the caller to call our asynchronous function and to capture a reference to the promise object. \n```\nvar promise = asyncFunction();\npromise.then(function(result){\n    // do something with result\n},\nfunction(error){\n    // do something with error\n}).then(...);\n```\nNote that the then function takes two arguments. Both of which are functions themselves. The argument that the promise API will send into these functions is what we resolve the rejected, the deferred object with accordingly. Result object and the error object could be just a simple string.\n\nThe `$q` service also has the capabilities to resolve multiple promises asynchronously, so no promise has to wait for another to complete in order to even start running. The `$q.all` method accomplishes that, plus the ability to have a central place to handle all the results or to handle any errors that come from any of the promises.\n```\n$q.all([promise1, promise2])\n.then(function(result){\n    // do something with result\n})\n.catch(function(error){\n    // handle error\n})\n```\n\n***\n#### _Summary_\n* Promises give us a lot of flexibility when dealing asynchronous behavior.\n* The `$q` service is the Angular implementation of Promise API.\n* Promises either get resolved or rejected.\n* The `.then` method takes 2 arguments (both function values):\n    * 1st - function to handle success or 'resolve' outcome;\n    * 2nd - function to handle error or 'reject' outcome;\n    * `.then` itself returns a Promise, so it chainable.\n* `$q.all` method allows us to execute multiole promises in parallel, handling success/failure in one central place.\n***\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnormandy72%2Fasynchronous-behavior","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnormandy72%2Fasynchronous-behavior","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnormandy72%2Fasynchronous-behavior/lists"}