{"id":22242485,"url":"https://github.com/asifzaman777/async-js-basic","last_synced_at":"2025-08-03T03:19:42.571Z","repository":{"id":204345755,"uuid":"709460748","full_name":"AsifZaman777/async-js-basic","owner":"AsifZaman777","description":"Asynchronous JS  ","archived":false,"fork":false,"pushed_at":"2023-10-29T20:28:43.000Z","size":10,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-01-30T09:41:53.406Z","etag":null,"topics":["async-await","call-stack","callback","callback-hell","error-handling","promise","promise-chain"],"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/AsifZaman777.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}},"created_at":"2023-10-24T19:07:14.000Z","updated_at":"2023-10-29T19:58:13.000Z","dependencies_parsed_at":null,"dependency_job_id":"b39f232e-f82c-4129-95d6-503111877d41","html_url":"https://github.com/AsifZaman777/async-js-basic","commit_stats":null,"previous_names":["asifzaman777/async-js-basic"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AsifZaman777%2Fasync-js-basic","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AsifZaman777%2Fasync-js-basic/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AsifZaman777%2Fasync-js-basic/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AsifZaman777%2Fasync-js-basic/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/AsifZaman777","download_url":"https://codeload.github.com/AsifZaman777/async-js-basic/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245449502,"owners_count":20617185,"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","call-stack","callback","callback-hell","error-handling","promise","promise-chain"],"created_at":"2024-12-03T04:16:25.795Z","updated_at":"2025-03-25T10:40:49.854Z","avatar_url":"https://github.com/AsifZaman777.png","language":"JavaScript","readme":"# Async Function:\n- Always return a promise\n- If we return some value within a async function it will wrap it up in promise and return the promise \n```\nasync function getData()\n{\n    return \"Hello Asif\";\n}\n\nlet data=getData(); // promise is returned\nconsole.log(getData());\n\n```\nin the second function Hello Asif will be wrapped up in a promise and will be returned as a promise.\n\n- More important thing: If we return a promise inside the async, then it will not wrap again into promise.\n\n```\n      let promise= new Promise((resolve,reject)=\u003e\n      {\n        resolve(\"I am promise\");\n      });\n\n      async function getData()\n      {\n        return promise;\n      }\n\n      getData().then((res)=\u003e console.log(res)); // we are getting the data inside the async function\n```\n\n\n# Async Await \n### Async Await is used to handle promises.\n\n- Normal promise handling  \n```\n const promise = new Promise((resolve,reject)=\u003e\n{\n    setTimeout(()=\u003e\n    {\n        resolve(\"I am a promise\");\n    },2000);\n});\npromise.then((res)=\u003econsole.log(res)); //normal promise handling\n\n```\n\n- Async Await promise handling\n\n```\nasync function promiseHandler()\n{\n  const p= await promise;\n  return p;\n}\n\npromiseHandler().then(res=\u003e console.log(res));\n\n```\n\n# Promise handling with Async Await VS Normal promise handling \n- Normal promise handling : As js is synchronous, non-blocking, single threaded lang. So js waits for none. \n\n```\nconst promise = new Promise((resolve,reject)=\u003e\n{\n   setTimeout(()=\u003e\n   {\n      resolve(\"I am a promise\");\n   },10000)\n});\n\n\n//normal promise handling\n\nfunction normalPromiseHandler()\n{\n  promise.then((res)=\u003econsole.log(res)); //print after 10s\n  console.log(\"Check difference between async and normal promise handling\"); // print immidiately.  \n}\nnormalPromiseHandler();\n```\n\n- JS synchronous nature can be changed by using `Async-await`. \n```\nconst promise = new Promise((resolve,reject)=\u003e\n{\n   setTimeout(()=\u003e\n   {\n      resolve(\"I am a promise\");\n   },10000)\n});\n\n\nasync function asyncPromiseHandler()\n{\n   const p = await promise;  //program waits here to promise to be resolved\n   console.log(p);   \n   console.log(\"Check difference between async and normal promise handling\"); \n\n   //both of the message will be printed after 10s when the promise is resolved\n}\n\nasyncPromiseHandler();\n```\n\n`Actually here the promgram waited for 10s to resolve the promise but it didn't block the program rather it halted the execution and after 10s when the promise resolved then both of the message executed`.\n\n\n# Non blocking nature of JS with Async-await (Interview concepts) \n\n- Who will be excecuted first? \n```\n\nconst promise = new Promise((resolve,reject)=\u003e\n{\n   setTimeout(()=\u003e\n   {\n      resolve(\"I am a promise\");\n   },10000)\n});\n\n\nasync function asyncPromiseHandler()\n{\n   const p1 = await promise;\n   console.log(p1);   //printed after 10s\n   console.log(\"Check text 1\");   //printed after 10s\n\n   const p2 = await promise;\n   console.log(p2);   //printed after 10s\n   console.log(\"Check text 2\");  //printed after 10s\n   \n}\n\nasyncPromiseHandler();\n\n```\n\n`In this program js halted the execution of p1 for 10s. But doesn't blocked the main thread or the operation. So meanwhile the p1 is resolving at that time the p2 will also be resolved as js waits for none (Non-blocking). But when the p1 is resolved then the rest of the texts and p2 will be executed together after 10s.`\n\n\n- Who will be excecuted first? \n```\n\nconst promise1 = new Promise((resolve,reject)=\u003e\n{\n   setTimeout(()=\u003e\n   {\n      resolve(\"I am a promise\");\n   },10000)\n});\n\nconst promise2 = new Promise((resolve,reject)=\u003e\n{\n   setTimeout(()=\u003e\n   {\n      resolve(\"I am a promise\");\n   },5000)\n});\n\n\n\nasync function asyncPromiseHandler()\n{\n   const p1 = await promise1; \n   console.log(p1);   // it will take 10s to be resolved \n   console.log(\"Check text 1\");  \n\n   const p2 = await promise2; // meanwhile p2 will be handled by js \n   console.log(p2);   \n   console.log(\"Check text 2\");  \n   \n   //after all the texts will be executed\n}\n\nasyncPromiseHandler();\n\n```\n\n`We have 2 promises to handle. promise1 needs 10s and promise2 needs 5s to be resolved. As p1 needs 10s program will halt the execution but meanwhile js will do its work and handle the rest of the texts and the p2 (which needs 5s to be resolved). After 10s when the p1 is resolved all the texts will be executed`\n\n- Who will be excecuted first?\n\n```\n\nconst promise1 = new Promise((resolve,reject)=\u003e\n{\n   setTimeout(()=\u003e\n   {\n      resolve(\"I am a promise\");\n   },5000)\n});\n\nconst promise2 = new Promise((resolve,reject)=\u003e\n{\n   setTimeout(()=\u003e\n   {\n      resolve(\"I am a promise\");\n   },10000)\n});\n\n\n\nasync function asyncPromiseHandler()\n{\n   const p1 = await promise1; \n   console.log(p1);   // it will take 5s to be resolved \n   console.log(\"Check text 1\");  \n\n   const p2 = await promise2; // meanwhile p2 will be handled by js \n   console.log(p2);   //it will be executed in the next 5s\n   console.log(\"Check text 2\");  //it will be executed in the next 5s\n   \n   //after all the texts will be executed\n}\n\nasyncPromiseHandler();\n\n\n```\n\n`Now in first 5s p1 will be handled and meanwhile p2 will also be handled. As p2 needs more 5s to be resolved so, it will be printed after the next 5s`\n\n\n\n\n\n\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fasifzaman777%2Fasync-js-basic","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fasifzaman777%2Fasync-js-basic","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fasifzaman777%2Fasync-js-basic/lists"}