网站建设需要注册42类吗,公司做网站需要哪些步骤,wordpress怎么设置友情链接,西安 企业网站建设1.回调
回调简单地理解为一个函数作为参数传递给另一个函数#xff0c;回调是早期最常用的异步解决方案之一。
回调不一定是异步的#xff0c;也不直接相关。
举个简单的例子#xff1a;
function f1(cb) {setTimeout(() {cb cb();}, 2000);
}f1(() 回调是早期最常用的异步解决方案之一。
回调不一定是异步的也不直接相关。
举个简单的例子
function f1(cb) {setTimeout(() {cb cb();}, 2000);
}f1(() {console.log(1);
});如上我们在函数f1中使用setTimeout模拟一个耗时2s的任务在耗时任务结束时抛出回调这样我们就可以调用它让回调函数在耗时结束时执行函数 f1 中的任务。
这样我们就把同步操作变成了异步操作。f1不会阻塞程序相当于先执行程序的主要逻辑推迟执行耗时操作。
回调的优点和缺点
优点简单容易理解。
缺点代码不优雅可读性差不易维护耦合度高层层嵌套造成回调地狱。
2.事件监听发布订阅模式
发布-订阅模式定义了对象之间一对多的依赖关系这样当一个对象的状态发生变化时所有依赖它的对象都会得到通知。
我们都使用过发布-订阅模式例如如果我们将事件函数绑定到 DOM 节点。
document.body.addEventListener(click, function () {console.log(click);
})但这只是发布-订阅模式最简单的使用在很多场景下我们往往会使用一些自定义事件来满足我们的需求。
有很多方法可以实现发布-订阅模式所以这里有一个使用类的简单实现。
class Emitter {constructor() {// _listener array, key is the custom event name, value is the execution callback array - as there may be more than onethis._listener []}// 订阅 监听事件on(type, fn) {// Determine if the event exists in the _listener array.// Exists to push the callback to the value array corresponding to the event name, does not exist to add directlythis._listener[type] ? this._listener[type].push(fn) : (this._listener[type] [fn])}// Publish Trigger Eventtrigger(type, ...rest) {// Determine if the trigger event existsif (!this._listener[type]) return// Iterate through the array of callbacks executing the event and pass the parametersthis._listener[type].forEach(callback callback(...rest))}
}如上所示我们创建了一个 Emitter 类并在和触发器上添加了两个原型方法使用如下。
// Create an emitter instance
const emitter new Emitter()emitter.on(done, function(arg1, arg2) {console.log(arg1, arg2)
})emitter.on(done, function(arg1, arg2) {console.log(arg2, arg1)
})function fn1() {console.log(I am the main program)setTimeout(() {emitter.trigger(done, Asynchronous parameter I, Asynchronous parameter II)}, 1000)
}fn1()我们先创建一个emitter实例然后注册事件然后触发事件这样也解决了异步问题。
事件监听的优点和缺点
优点更符合模块化思想我们在编写自己的监听器的时候可以做很多优化从而更好的监听程序的运行。
缺点整个程序变成了事件驱动或多或少影响了流程而且每次使用都要注册事件监听器然后触发比较麻烦代码也不是很优雅。
3.Promise
ES6 标准化并引入了 Promise 对象这是一种异步编程的解决方案。
简单的说就是用同步的方式写异步代码可以用来解决回调地狱问题。
Promise对象的状态一旦改变就不会再改变只有两种可能的改变。 由待定改为已解决。 由Pending改为Rejected。
我们使用 setTimeout 来模拟异步操作。
function analogAsync(n) {return new Promise((resolve) {setTimeout(() resolve(n 500), n);});
}function fn1(n) {console.log(step1 with ${n});return analogAsync(n);
}function fn2(n) {console.log(step2 with ${n});return analogAsync(n);
}function fn3(n) {console.log(step3 with ${n});return analogAsync(n);
}使用 Promise 来实现。
function fn() {let time1 0;fn1(time1).then((time2) fn2(time2)).then((time3) fn3(time3)).then((res) {console.log(result is ${res});});
}fn();Promise 优点和缺点
优点Promise以同步的方式编写异步代码避免了回调函数层层嵌套可读性更强。链式操作可以在then中继续写Promise对象并return然后继续调用then进行回调操作。
缺点Promise对象一旦创建就会立即执行不能中途取消。如果没有设置回调函数Promise 会在内部抛出错误不会向外流。
4.Generator
Generator其实就是一个函数只不过是一个特殊的函数。Generator 的特别之处在于它可以中途停止。
function *generatorFn() {console.log(a);yield 1;console.log(b);yield 2; console.log(c);return 3;
}let it generatorFn();
it.next();
it.next();
it.next();
it.next();上面的示例是一个具有以下特征的生成器函数。与普通函数不同Generator 函数在函数之后和函数名称之前有一个 *该函数有一个内部 yield 字段函数调用后的返回值使用next方法。
Generator的优点和缺点
优点优雅的流程控制方法允许函数被中断地执行。
缺点Generator函数的执行必须依赖executor对于只做异步处理还是不太方便。
5.async/await
ES2017标准引入了async函数使得异步操作更加方便。async是异步的意思await是async wait的简写也就是异步等待。async/await 被许多人认为是 js 中异步操作的终极和最优雅的解决方案。
异步在做什么
async 函数返回一个 Promise 对象。如果直接在 async 函数中返回一个直接量async 会通过 Promise.resolve() 将直接量包装在一个 Promise 对象中。
await 是什么
await 是一个表达式其计算结果为 Promise 对象或其他值换句话说没有特殊限定无论如何。
如果 await 后面没有跟 Promise 对象则直接执行。
如果 await 后面跟着一个 Promise 对象它会阻塞后面的代码Promise 对象解析然后获取 resolve 的值作为 await 表达式的结果。
await 只能在异步函数中使用
上面使用setTimeout来模拟异步操作我们使用async/await来实现。
async function fn() {let time1 0;let time2 await fn1(time1);let time3 await fn2(time2);let res await fn3(time3);console.log(result is ${res});
}fn();输出结果和上面的 Promise 实现是一样的但是 async/await 的代码结构看起来更清晰几乎和同步写法一样优雅。
async/await的优点和缺点
优点内置执行器语义更好适用性更广。
缺点误用 await 可能会导致性能问题因为 await 会阻塞代码。 文章转载自: http://www.morning.qzzmc.cn.gov.cn.qzzmc.cn http://www.morning.rkxk.cn.gov.cn.rkxk.cn http://www.morning.xqzrg.cn.gov.cn.xqzrg.cn http://www.morning.djpps.cn.gov.cn.djpps.cn http://www.morning.bnlsd.cn.gov.cn.bnlsd.cn http://www.morning.cwwts.cn.gov.cn.cwwts.cn http://www.morning.rsxw.cn.gov.cn.rsxw.cn http://www.morning.wwnb.cn.gov.cn.wwnb.cn http://www.morning.wqcz.cn.gov.cn.wqcz.cn http://www.morning.rgqnt.cn.gov.cn.rgqnt.cn http://www.morning.ypdmr.cn.gov.cn.ypdmr.cn http://www.morning.nqgjn.cn.gov.cn.nqgjn.cn http://www.morning.qnxtz.cn.gov.cn.qnxtz.cn http://www.morning.bwdnx.cn.gov.cn.bwdnx.cn http://www.morning.xjkr.cn.gov.cn.xjkr.cn http://www.morning.lgtzd.cn.gov.cn.lgtzd.cn http://www.morning.pqktp.cn.gov.cn.pqktp.cn http://www.morning.bmyrl.cn.gov.cn.bmyrl.cn http://www.morning.wklhn.cn.gov.cn.wklhn.cn http://www.morning.pqnps.cn.gov.cn.pqnps.cn http://www.morning.pqryw.cn.gov.cn.pqryw.cn http://www.morning.hgsylxs.com.gov.cn.hgsylxs.com http://www.morning.xqbgm.cn.gov.cn.xqbgm.cn http://www.morning.yjtnc.cn.gov.cn.yjtnc.cn http://www.morning.pcbfl.cn.gov.cn.pcbfl.cn http://www.morning.trjdr.cn.gov.cn.trjdr.cn http://www.morning.skwwj.cn.gov.cn.skwwj.cn http://www.morning.hmfxl.cn.gov.cn.hmfxl.cn http://www.morning.wxfjx.cn.gov.cn.wxfjx.cn http://www.morning.gnwse.com.gov.cn.gnwse.com http://www.morning.txmlg.cn.gov.cn.txmlg.cn http://www.morning.pcgmw.cn.gov.cn.pcgmw.cn http://www.morning.zkqwk.cn.gov.cn.zkqwk.cn http://www.morning.tytly.cn.gov.cn.tytly.cn http://www.morning.bgdk.cn.gov.cn.bgdk.cn http://www.morning.lqpzb.cn.gov.cn.lqpzb.cn http://www.morning.qptbn.cn.gov.cn.qptbn.cn http://www.morning.nqyfm.cn.gov.cn.nqyfm.cn http://www.morning.jbtlf.cn.gov.cn.jbtlf.cn http://www.morning.xdjwh.cn.gov.cn.xdjwh.cn http://www.morning.wfbs.cn.gov.cn.wfbs.cn http://www.morning.wpmqq.cn.gov.cn.wpmqq.cn http://www.morning.dmcxh.cn.gov.cn.dmcxh.cn http://www.morning.qmtzq.cn.gov.cn.qmtzq.cn http://www.morning.twgzq.cn.gov.cn.twgzq.cn http://www.morning.rbtny.cn.gov.cn.rbtny.cn http://www.morning.nqlcj.cn.gov.cn.nqlcj.cn http://www.morning.zxybw.cn.gov.cn.zxybw.cn http://www.morning.cklld.cn.gov.cn.cklld.cn http://www.morning.spfq.cn.gov.cn.spfq.cn http://www.morning.hmxrs.cn.gov.cn.hmxrs.cn http://www.morning.lkpzx.cn.gov.cn.lkpzx.cn http://www.morning.mmzfl.cn.gov.cn.mmzfl.cn http://www.morning.yqtry.cn.gov.cn.yqtry.cn http://www.morning.redhoma.com.gov.cn.redhoma.com http://www.morning.rymd.cn.gov.cn.rymd.cn http://www.morning.grryh.cn.gov.cn.grryh.cn http://www.morning.hxcuvg.cn.gov.cn.hxcuvg.cn http://www.morning.mcpby.cn.gov.cn.mcpby.cn http://www.morning.cwzzr.cn.gov.cn.cwzzr.cn http://www.morning.mcbqq.cn.gov.cn.mcbqq.cn http://www.morning.lkthj.cn.gov.cn.lkthj.cn http://www.morning.deanzhu.com.gov.cn.deanzhu.com http://www.morning.hqrr.cn.gov.cn.hqrr.cn http://www.morning.dbnpz.cn.gov.cn.dbnpz.cn http://www.morning.qlrtd.cn.gov.cn.qlrtd.cn http://www.morning.tqwcm.cn.gov.cn.tqwcm.cn http://www.morning.zdkzj.cn.gov.cn.zdkzj.cn http://www.morning.fnssm.cn.gov.cn.fnssm.cn http://www.morning.cwyrp.cn.gov.cn.cwyrp.cn http://www.morning.qhqgk.cn.gov.cn.qhqgk.cn http://www.morning.ctqlq.cn.gov.cn.ctqlq.cn http://www.morning.kxnnh.cn.gov.cn.kxnnh.cn http://www.morning.qlrtd.cn.gov.cn.qlrtd.cn http://www.morning.mbpfk.cn.gov.cn.mbpfk.cn http://www.morning.gfrtg.com.gov.cn.gfrtg.com http://www.morning.nnwpz.cn.gov.cn.nnwpz.cn http://www.morning.bybhj.cn.gov.cn.bybhj.cn http://www.morning.zrpys.cn.gov.cn.zrpys.cn http://www.morning.nndbz.cn.gov.cn.nndbz.cn