栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

无阻塞同步AJAX

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

无阻塞同步AJAX

在即将到来的ECMAscript
2016(ES7)标准中,提供了一组新的语言关键字,这些关键字旨在执行与您似乎正在寻找的目标非常相似的操作,称为

async
await

这些关键字 不会 允许“非阻塞同步AJAX”,但他们让你的方式,编写异步代码 看起来 同步。这是一个简单的例子:

// Let's say you have an asynchronous function that you want to call in a synchronous// style...function delayedeval(delay, func) {  // First, ensure that the function returns a Promise object. If it doesn't, wrap it with  // another function that does.  return new Promise((resolve, reject) => {    setTimeout(() => resolve(func()), delay)  })  // For more on Promises, see https://goo.gl/uaoDuy (MDN link)}// Then, declare a function as asynchronous. This causes it to return a Promise that // resolves to its return value, instead of returning its return value directly.async function delayedHello() {  // Inside an async function, you can call other async functions like this, which looks  // very much like a synchronous call (even though it isn't).  let message = await delayedeval(1500, () => "Hello, world!")  console.log(message)}// This returns (a Promise) immediately, but doesn't print "Hello, world!" until 1.5// seconds later. (At which point it resolves the Promise.)delayedHello()

尝试通天塔

基本上,而不是“无不良副作用同步AJAX”,

async
await
让你 异步 AJAX没有所有
负面影响。(具有许多用于处理回调的逻辑的混乱代码。)

async
并且
await
是ES7标准中“异步功能”候选推荐的一部分。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/392369.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号