人与人之间联系更加紧密的互联网
是什么成就了今天的 Web 2.0?
$
特点:不太复杂
挑战:程序员多
尽可能少但又足够的并发
$.ajax(
{
url: 'https://jyywiki.cn/hello/jyy',
success: function(resp) {
console.log(resp);
},
error: function(req, status, err) {
console.log("Error");
}
}
);
好处
坏处
$.ajax
嵌套 5 层,可维护性已经接近于零了导致 callback hell 的本质:人类脑袋里想的是 “流程图”,看到的是 “回调”。
The Promise object represents the eventual completion (or failure) of an asynchronous operation and its resulting value.
Chaining
loadScript("/article/promise-chaining/one.js")
.then( script => loadScript("/article/promise-chaining/two.js") )
.then( script => loadScript("/article/promise-chaining/three.js") )
.then( script => {
// scripts are loaded, we can use functions declared there
})
.catch(err => { ... } );
Fork-join
a = new Promise( (resolve, reject) => { resolve('A') } )
b = new Promise( (resolve, reject) => { resolve('B') } )
c = new Promise( (resolve, reject) => { resolve('C') } )
Promise.all([a, b, c]).then( res => { console.log(res) } )
async function
Promise
objectasync_func()
- forkawait promise
await promise
- joinA = async () => await $.ajax('/hello/a')
B = async () => await $.ajax('/hello/b')
C = async () => await $.ajax('/hello/c')
hello = async () => await Promise.all([A(), B(), C()])
hello()
.then(window.alert)
.catch(res => { console.log('fetch failed!') } )