匠人学院 JR Academy
  • Position

    我现在在哪,该往哪走?

    几道题看你现在的岗位受 AI 影响多大、机会在哪。免费,不用注册。

    免费看看我现在在哪

    AI 对职业的影响

    我的工作会被 AI 替代吗常见职业的 AI 影响与转型方向AI 时代的新岗位做什么、要求什么、怎么转过去AI 影响自测几道题看你所在岗位的风险和机会

    测一测

    能力测评AI Engineer 面试准备度自测职业性格测试找到和你性格匹配的方向职业规划回答几个问题,看可能适合的职业回国求职评估按学校、专业、毕业时间看回国机会
  • Skill

    不知道该学什么 AI?

    先按方向免费学,每个方向从入门讲到动手做;想系统学再看训练营和导师。

    按方向免费开始学

    按方向免费学

    Vibe Coding用 AI 写代码,零基础可学AI 办公提效文档、表格、汇报用 AI 省时间Prompt 大师把需求说清楚的写法AI EngineerRAG、Agent、MCP 工程实践AI 数据分析用 AI 做查询、清洗和报表全部学习方向 →

    训练营与课程

    全部训练营按职业方向、难度、形式筛选AI Engineer Bootcamp从 LLM 应用到 Agent 的工程训练营人人都能学的 AI Coding非技术背景的 AI 编程实战AI 工作流提效营在职人士的 AI 工作流导师团队一线工程师与行业导师全部课程 →

    动手练与资料

    互动 LabPrompt、前端、Python、AWS 在线练学习路线图一张图看清该按什么顺序学Cheat Sheets语言与工具速查表Wiki 知识库AI 工具与概念讲解视频讲座、公开课、学员分享免费资源 →
  • Evidence

    拿什么让雇主相信你?

    证书、项目、团队实战、比赛作品,最后整理成一份作品档案。

    了解作品档案

    考证

    考证匠 · 全部认证AWS、Azure、GCP、Claude 等备考与模考AWS SAA-C03云架构师入门认证Azure AI-102微软 AI 工程师认证Claude 认证Anthropic Claude 系列认证模拟考试按真实考试界面限时练全部认证 →

    项目与团队实战

    P3 团队实战和真人协作做项目,模拟实习式的团队流程P3 项目经历P3 项目能写进简历的部分Demo Day项目成果展示P3 证书完成项目后的证明P3 全部 →

    比赛与作品

    黑客松限时做出一个作品提交作品把你的项目放进作品展示CareerOS 作品档案在桌面版做任务,提交的结果整理成作品
    考证匠浏览器插件 + App + 小程序截屏分析考题,随时刷题了解考证匠
  • Connect

    怎么把自己推出去?

    简历、面试、内推、导师。先看你和目标岗位差多少,再决定从哪补。

    用求职匠开始

    简历与机会

    毕业生求职澳洲、中国、马来西亚、新加坡、美国、英国求职准备求职匠AI 求职工作台:改简历、找岗位、投递AI 简历对照 JD 改写简历内推机会学员和导师分享的内推岗位机会实习、项目和合作机会Offer 榜学员拿到的 Offer

    面试

    AI 模拟面试文字或语音练一次,看差在哪面试中心按公司和岗位找题面经真实面试经历System Design 手册系统设计面试准备面试全部 →

    导师

    1v1 导师按小时约 AI、求职、技术导师拼团私教几个人一起上,每人更便宜职业辅导服务简历、面试、方向的辅导套餐成为导师加入匠人导师团队
    求职匠桌面版一个工作台管完整求职在 macOS/Windows 整理真实经历,按岗位完善简历版本。了解求职匠
  • Community

    去哪认识同路的人?

    讲座、公开课、Workshop、黑客松,线上线下都有。

    看近期活动

    活动类型

    讲座行业嘉宾分享公开课免费试听课程内容Workshop动手做一个小作品黑客松组队限时开发新生节开学季的校园活动

    城市

    悉尼悉尼的线下活动墨尔本墨尔本的线下活动布里斯班布里斯班的线下活动线上在哪都能参加全部活动 →

    社群

    职业社区求职者互助学习小组找人一起学Discord在线交流群
企业会员下载 CareerOS
0
匠人学院 JR Academy学AI来匠人

Follow Us

linkedinfacebooktwitterinstagramweiboyoutubebilibilitiktokxigua

We Accept

/image/layout/pay-paypal.png/image/layout/pay-visa.png/image/layout/pay-master-card.png/image/layout/pay-airwallex.png/image/layout/pay-alipay.png
EN
产品

CareerOS 与工具

  • CareerOS
  • 考证匠
  • 求职匠
  • 求职匠网页版
  • 求职匠分析报告
  • 求职匠桌面版下载
  • 我的求职报告
  • 购买报告次数
  • 求职匠账单
  • AI 工具箱
  • AI Skill 商城

会员与课程

  • 会员
  • AI 会员
  • AI Engineer VIP
  • 训练营
  • Training Camp
  • 看板学习
  • AI Builder 课程
  • MCP 课程
  • NotebookLM Workshop
  • Business Analyst 课程
  • Web全栈班
  • DevOps项目班
  • 数据工程全栈班
  • 数据分析项目班
  • 编程入门班
  • 算法集训营

求职服务

  • 毕业生求职
  • 澳洲毕业生求职
  • 中国毕业生求职
  • 马来西亚毕业生求职
  • 新加坡毕业生求职
  • 美国毕业生求职
  • 英国毕业生求职
  • 1v1 辅导
  • 职业辅导
  • BA和产品经理实习
  • 数据科学实习
  • 数据分析实习
  • Marketing实习
  • 简历修改
  • 面试指导
  • 岗位代投
  • 职位监控
  • LinkedIn代运营
  • LinkedIn人脉代加
内容库

学习方向

  • 全部学习方向
  • AI Engineer
  • Context Engineering
  • Vibe Coding
  • Prompt Master
  • AI Builder
  • AI 产品经理
  • Python 入门
  • AI 办公提效
  • AI 数据分析
  • AI 财务
  • AI 内容创作
  • AI 视觉创作
  • 前端开发
  • Hermes Agent
  • OpenClaw 本地智能体

互动 Lab

  • Prompt Lab
  • Frontend Lab
  • JavaScript Lab
  • Python Lab
  • AWS Lab
  • Azure Lab
  • LLM Lab
  • Git Lab

资料与指南

  • Wiki 知识库
  • 学习 Wiki
  • Cheat Sheets
  • 学习路线图
  • AI 路线图
  • 视频
  • 博客
  • 面试手册
  • 模拟考试
  • 免费资源

城市

  • 悉尼
  • 墨尔本
  • 布里斯班
  • 阿德莱德
  • 新加坡
  • 吉隆坡
  • 伦敦
  • 纽约
  • 旧金山
  • 洛杉矶
  • 西雅图
  • 多伦多
  • 温哥华

移民

  • 澳洲移民
  • 技术移民189/190/491
  • 雇主担保482/186/494
  • 投资移民188/888
  • 英国移民
  • 美国移民
  • 加拿大移民
  • 新加坡移民
  • 马来西亚移民
企业

企业服务

  • AI for Business
  • 企业门户登录
  • 企业培训
  • 实习合作
  • 招聘合作
  • Enterprise (EN)

Metatree AI Lab

  • Metatree AI Lab
  • AI 解决方案
  • 数字员工
  • 案例
  • Metatree AI Lab(英文)
  • AI Services(英文)
  • Digital Employee(英文)
  • Work(英文)

青少年 AI 教育

  • Airbotix 少儿 AI 编程
  • 澳洲家长实用资料库
  • NAPLAN 成绩单怎么看
  • My School 学校数据指南
  • 悉尼私校学费 2026
  • 少儿编程课程与训练营
关于

公司

  • 关于我们
  • 匠人导师
  • 成为导师
  • 新闻资讯
  • 元宇宙课堂
  • 匠人工作
  • 匠人商店J3.Club
  • 联系我们

帮助与条款

  • 常见问题
  • 服务条款
  • 隐私政策
  • 取消与退款政策
  • 网站地图

友情链接

  • CS Browser

联系

  • hello@jiangren.com.au
  • 0421-672-555

地址

Level 10b, 144 Edward Street, Brisbane CBD (Headquarter)
Level 2, 171 La Trobe St, Melbourne VIC 3000
四川省成都市武侯区桂溪街道天府大道中段500号D5东方希望天祥广场B座45A13号
Business Hub, 155 Waymouth St, Adelaide SA 5000

Disclaimer

footer-disclaimerfooter-disclaimer

JR Academy acknowledges Traditional Owners of Country throughout Australia and recognises the continuing connection to lands, waters and communities. We pay our respect to Aboriginal and Torres Strait Islander cultures; and to Elders past and present. Aboriginal and Torres Strait Islander peoples should be aware that this website may contain images or names of people who have since passed away.

匠人学院网站上的所有内容,包括课程材料、徽标和匠人学院网站上提供的信息,均受澳大利亚政府知识产权法的保护。严禁未经授权使用、销售、分发、复制或修改。违规行为可能会导致法律诉讼。通过访问我们的网站,您同意尊重我们的知识产权。JR Academy Pty Ltd 保留所有权利,包括专利、商标和版权。任何侵权行为都将受到法律追究。查看用户协议

© 2017-2026 JR Academy Pty Ltd. All rights reserved.

ABN 26621887572

首页学习AI课程考证
  1. 首页
  2. 学技能
  3. 资源中心
  4. Asynchronous JavaScript: From Callback Hell to Async and Await
JR Academy · Blog职业洞察

Asynchronous JavaScript: From Callback Hell to Async and Await

Asynchronous JavaScript: From Callback Hell to Async and Await.Synchronizing asynchronous tasks in JavaScript was a serious issue for a very long time.

发布日期2018-07-24
阅读时长5 分钟
作者JiangRen Mr

快速导航

  • A Brief History of Asychronous JavaScript
  • Approach 1: Callback Hell (“The Pyramid of Doom”)
  • JavaScript Promises
  • Approach 3: Async/Await
  • Async - A Long Awaited Resolution of a Promise

One of the keys to writing a successful web application is being able to make dozens of AJAX calls per page.

This is a typical asynchronous programming challenge, and how you choose to deal with asynchronous calls will, in large part, make or break your app, and by extension potentially your entire startup.

Synchronizing asynchronous tasks in JavaScript was a serious issue for a very long time.

 

A Brief History of Asychronous JavaScript

The first and the most straightforward solution came in the form of nested functions as callbacks. This solution led to something called callback hell, and too many applications still feel the burn of it.

Then, we got Promises. This pattern made the code a lot easier to read, but it was a far cry from the Don’t Repeat Yourself (DRY) principle. There were still too many cases where you had to repeat the same pieces of code to properly manage the application’s flow. The latest addition, in the form of async/await statements, finally made asynchronous code in JavaScript as easy to read and write as any other piece of code.

Let’s take a look at the examples of each of these solutions and reflect on the evolution of asynchronous programming in JavaScript.

To do this, we will examine a simple task that performs the following steps:

  1. Verify the username and password of a user.
  2. Get application roles for the user.
  3. Log application access time for the user.

Approach 1: Callback Hell (“The Pyramid of Doom”)

The ancient solution to synchronize these calls was via nested callbacks. This was a decent approach for simple asynchronous JavaScript tasks, but wouldn’t scale because of an issue called callback hell.

The code for the three simple tasks would look something like this:

const verifyUser = function(username, password, callback){
   dataBase.verifyUser(username, password, (error, userInfo) => {
       if (error) {
           callback(error)
       }else{
           dataBase.getRoles(username, (error, roles) => {
               if (error){
                   callback(error)
               }else {
                   dataBase.logAccess(username, (error) => {
                       if (error){
                           callback(error);
                       }else{
                           callback(null, userInfo, roles);
                       }
                   })
               }
           })
       }
   })
};

Each function gets an argument which is another function that is called with a parameter that is the response of the previous action.

Too many people will experience brain freeze just by reading the sentence above. Having an application with hundreds of similar code blocks will cause even more trouble to the person maintaining the code, even if they wrote it themselves.

This example gets even more complicated once you realize that a database.getRoles is another function that has nested callbacks.

const getRoles = function (username, callback){
   database.connect((connection) => {
       connection.query('get roles sql', (result) => {
           callback(null, result);
       })
   });
};

In addition to having code that is difficult to maintain, the DRY principle has absolutely no value in this case. Error handling, for example, is repeated in each function and the main callback is called from each nested function.

More complex asynchronous JavaScript operations, such as looping through asynchronous calls, is an even bigger challenge. In fact, there is no trivial way of doing this with callbacks. This is why JavaScript Promise libraries like Bluebird and Q got so much traction. They provide a way to perform common operations on asynchronous requests that the language itself doesn’t already provide.

That’s where native JavaScript Promises come in.

JavaScript Promises

Promises were the next logical step in escaping callback hell. This method did not remove the use of callbacks, but it made the chaining of functions straightforward and simplified the code, making it much easier to read.

Illustration: Asynchronous JavaScript Promises diagram

 With Promises in place, the code in our asynchronous JavaScript example would look something like this:

const verifyUser = function(username, password) {
   database.verifyUser(username, password)
       .then(userInfo => dataBase.getRoles(userInfo))
       .then(rolesInfo => dataBase.logAccess(rolesInfo))
       .then(finalResult => {
           //do whatever the 'callback' would do
       })
       .catch((err) => {
           //do whatever the error handler needs
       });
};

To achieve this kind of simplicity, all of the functions used in the example would have to be Promisified. Let’s take a look at how the getRoles method would be updated to return a Promise:

const getRoles = function (username){
   return new Promise((resolve, reject) => {
       database.connect((connection) => {
           connection.query('get roles sql', (result) => {
               resolve(result);
           })
       });
   });
};

We have modified the method to return a Promise, with two callbacks, and the Promise itself performs actions from the method. Now, resolve and reject callbacks will be mapped to Promise.then and Promise.catch methods respectively.

You may notice that the getRoles method is still internally prone to the pyramid of doom phenomenon. This is due to the way database methods are created as they do not return Promise. If our database access methods also returned Promise the getRoles method would look like the following:

const getRoles = new function (userInfo) {
   return new Promise((resolve, reject) => {
       database.connect()
           .then((connection) => connection.query('get roles sql'))
           .then((result) => resolve(result))
           .catch(reject)
   });
};

Approach 3: Async/Await

JavaScript is asynchronous by default. This might be reason why it took so long to get synchronous-looking code that runs properly in JavaScript. But, better late than never! The pyramid of doom was significantly mitigated with the introduction of Promises. However, we still had to rely on callbacks that are passed on to .then and .catch methods of a Promise.

Promises paved the way to one of the coolest improvements in JavaScript. ECMAScript 2017 brought in syntactic sugar on top of Promises in JavaScript in the form of async and await statements.

They allow us to write Promise-based code as if it were synchronous, but without blocking the main thread, as this code sample demostrates:

const verifyUser = async function(username, password){
   try {
       const userInfo = await dataBase.verifyUser(username, password);
       const rolesInfo = await dataBase.getRoles(userInfo);
       const logStatus = await dataBase.logAccess(userInfo);
       return userInfo;
   }catch (e){
       //handle errors as needed
   }
};

Awaiting Promise to resolve is allowed only within async functions which means that verifyUser had to be defined using async function.

However, once this small change is made you can await any Promise without additional changes in other methods.

Async - A Long Awaited Resolution of a Promise

Async functions are the next logical step in the evolution of asynchronous programming in JavaScript. They will make your code much cleaner and easier to maintain. Declaring a function as async will ensure that it always returns a Promise so you don’t have to worry about that anymore.

Why should you start using the JavaScript async function today?

  1. The resulting code is much cleaner.
  2. Error handling is much simpler and it relies on try/catch just like in any other synchronous code.
  3. Debugging is much simpler. Setting a breakpoint inside a .then block will not move to the next .thenbecause it only steps through synchronous code. But, you can step through await calls as if they were synchronous calls.

 

 

 

 

Front-end
作者JiangRen Mr
一键分享或复制链接
作者: JiangRen Mr查看该作者的更多文章 →

相关学习资源

  • AI 学习中心
  • Prompt 工程入门
  • 前端学习路径
← 上一篇2018年6月30日布里斯班大咖面对面活动集锦下一篇 →IT杂谈|“黑五”明天到,购物不剁手|全澳最全打折信息看这里

相关文章推荐

Atlassian 前端面试流程详细解读

2021-12-10

第一份工作就年薪80k,毫无工作经验的他是怎么做到的?

2019-11-21

如何参与一个顶级开源项目

2019-09-05

如何使用useReducer Hook

2019-09-05

作为一个前端工程师也要掌握的几种文件路径知识

2019-09-05

如何写一个让面试官满意的 Generator 执行器?

2019-09-05
查看全部文章 →

看完了,下一步

不知道下一步学什么?说说你的情况

说说你现在做什么、想去哪,按技能、证据、经验、表达、定位五类看差在哪、先补什么。不用登录。

对照我的情况
  • 技能按方向免费学 AIVibe Coding、AI 办公、Prompt、AI Engineer 等方向,按章节从入门学起。看全部学习方向 →
  • 定位按岗位看学习路线图前端、后端、数据、AI 等方向,按阶段列出要学的内容和先后顺序。看全部路线图 →
  • 表达练一次 AI 模拟面试选一个岗位开始模拟面试,练习把经历和项目讲清楚。去练一次 →

不确定下一步做什么?

带上这页的主题,对照你的情况看差在哪。

对照我的情况
JR Academy
全球华人学习 AI 第一站
训练营免费资源AI学习职业辅导
精选推荐
AI 职业影响地图
测测你的职业风险等级,查看转型路径与学习方向
热门工具
AI & 数据训练营
系统化课程 + 真实项目实战,快速提升竞争力
热门课程
1v1 就业辅导
资深导师一对一指导,简历优化 + 面试准备
职业辅导
企业内训定制
AI 技能培训方案,助力团队升级
企业服务
热门标签
Vibe CodingAI 编程CursorClaude求职攻略Prompt前端开发后端开发
订阅更新

获取最新 AI 学习资源、技术教程和求职攻略,直接送达邮箱。

我们尊重您的隐私,不会发送垃圾邮件