xgqfrms™, xgqfrms® : xgqfrms's offical website of cnblogs! xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!

What's the difference between Async Await and Promise in JavaScript All In One

What's the difference between Async Await and Promise in JavaScript All In One

Async vs Promise

image

image

demos

async function declaration

// named async function declaration
async function asyncFunctionDeclaration(param0, param1, /* …, */ paramN) {
  // statements
  console.log(`anonymous async function declaration object`)
}


// anonymous async function declaration IIFE
(async function (param0, param1, /* …, */ paramN) {
  // statements
  console.log(`anonymous async function declaration IIFE`)
  return Promise.resolve(`anonymous async function declaration IIFE`)
})().then(msg => {
  console.log(`anonymous msg =`, msg)
  return msg;
})

// named async function declaration IIFE
(async function iife(param0, param1, /* …, */ paramN) {
  // statements
  console.log(`named async function declaration IIFE`)
  return Promise.resolve(`named async function declaration IIFE`)
})().then(msg => {
  console.log(`named msg =`, msg)
  return msg;
})

async function expression

// anonymous async function expression class
class AnonymousAsyncFunctionClass {
  constructor() {
    const add = async function (param0, param1, /* …, */ paramN) {
      // statements
      console.log(`anonymous async function expression class`)
    }
    this.add = add;
  }
  anonym = async function (param0, param1, /* …, */ paramN) {
    // statements
  }
  static test() {
    //
  }
  es5 = function () {
    //
  }
  es6 = () => {
    //
  }
}

// maned async function expression class
class NamedAsyncFunctionClass {
  constructor() {
    const add = async function add(param0, param1, /* …, */ paramN) {
      // statements
      console.log(`named async function expression class`)
    }
    this.add = add;
  }
  named = async function named(param0, param1, /* …, */ paramN) {
    // statements
  }
  static test() {
    //
  }
  es5 = function () {
    //
  }
  es6 = () => {
    //
  }
}


// anonymous async function expression object
const AnonymousAsyncFunctionObject = {
  add: async function (param0, param1, /* …, */ paramN) {
    // statements
    console.log(`anonymous async function expression object`)
  },
}

// named async function expression object
const namedAsyncFunctionObject = {
  add: async function add(param0, param1, /* …, */ paramN) {
    // statements
    console.log(`named async function expression object`)
  },
}


// anonymous async function expression IIFE
const anonymousIIFE = (async function (param0, param1, /* …, */ paramN) {
  // statements
  console.log(`anonymous async function expression IIFE`)
  return Promise.resolve(`anonymous async function expression IIFE`)
})().then(msg => {
  console.log(`anonymous msg =`, msg)
  return msg;
})
anonymousIIFE.then(str => console.log(`anonymous str =`, str))

// named async function expression IIFE
const namedIIFE = (async function iife(param0, param1, /* …, */ paramN) {
  // statements
  console.log(`named async function expression IIFE`)
  return Promise.resolve(`named async function expression IIFE`)
})().then(msg => {
  console.log(`named msg =`, msg)
  return msg;
})
namedIIFE.then(str => console.log(`named str =`, str))

Promise

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise

Async

The async function declaration creates a binding of a new async function to a given name.
The await keyword is permitted within the function body, enabling asynchronous, promise-based behavior to be written in a cleaner style and avoiding the need to explicitly configure promise chains.

简化 Promise 链式调用的显式配置,写法更优雅

function resolveAfter2Seconds() {
  return new Promise((resolve) => {
    console.log('wait1...');
    setTimeout(() => {
      console.log('wait3...', typeof resolve);
      resolve('resolved');
      console.log('wait4...');
    }, 2000);
    console.log('wait2...');
  });
}

// async function declaration ✅
async function asyncCall() {
  console.log('calling 0 ');
  const result = await resolveAfter2Seconds();
  console.log(`result 5`, result);
}

asyncCall();

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function

The async function keywords can be used to define an async function inside an expression.

// async function expression

function resolveAfter2Seconds(x) {
  return new Promise((resolve) => {
    setTimeout(() => {
      resolve(x);
    }, 2000);
  });
}

// async function expression assigned to a variable
const add = async function (x) {
  const a = await resolveAfter2Seconds(20);
  const b = await resolveAfter2Seconds(30);
  return x + a + b;
};

add(10).then((v) => {
  console.log(v); // prints 60 after 4 seconds.
});

// async function expression used as an IIFE
(async function (x) {
  const p1 = resolveAfter2Seconds(20);
  const p2 = resolveAfter2Seconds(30);
  return x + (await p1) + (await p2);
})(10).then((v) => {
  console.log(v); // prints 60 after 2 seconds.
});


https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/async_function


https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function*

Await


https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/await

Zero Width Joiner / 零宽连接器

零宽字符 / Zero Width Space

U+200D
Zero width joiner
<ZWJ>
Placed between characters that would not normally be connected in order to cause the characters to be rendered using their connected form in certain languages.

image

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Lexical_grammar#automatic_semicolon_insertion

https://en.wikipedia.org/wiki/Zero-width_joiner

👨‍👩‍👦‍👦

组合 emoji 字符串反转,防止分开

// 

https://www.cnblogs.com/xgqfrms/p/17666225.html

Man] [ZWJ] [Woman] [ZWJ] [Boy] => 👨‍👩‍👦 => Family: Man, Woman, Boy

https://en.wikipedia.org/wiki/Zero-width_joiner

refs

https://javascript.info/async-await

https://www.codingninjas.com/studio/library/difference-between-promise-and-async-await



©xgqfrms 2012-2025

www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!

原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!


posted @   xgqfrms  阅读(33)  评论(3编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· C#/.NET/.NET Core技术前沿周刊 | 第 29 期(2025年3.1-3.9)
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
历史上的今天:
2022-08-31 如何把闲置的 Mac mini 搭建成一个局域网中的 Web 服务器 All In One
2022-08-31 macOS & VSCode 批量替换快捷键 All In One
2021-08-31 js for css pseudo element All In One
2021-08-31 element-ui 表单验证 多个 tabs 校验规则复用 bug All In One
2020-08-31 node --experimental-modules & node.js ES Modules
2020-08-31 ESLint All In One
2020-08-31 Omegle
点击右上角即可分享
微信分享提示