Pramise
function getData( url ) {
return new Promise( ( resolve, reject ) => {
$.get( {
url: url,
success: function ( data ) {
resolve( data )
}
} )
} )
}
getData( "http://jsonplaceholder.typicode.com/posts?userId=2" ).then( () => {
console.log( 111 )
return getData( "http://jsonplaceholder.typicode.com/posts?userId=1" )
} ).then( ( data ) => {
console.log( 222 )
console.log( data )
} )
promise 對象異步操作成功之后,將then方法的函數(shù)傳入作為resolve回調(diào)函數(shù)調(diào)用,第一個(gè)then方法作為回調(diào)函數(shù)調(diào)用之后又返回了一個(gè)promise對象,既可以鏈?zhǔn)秸{(diào)用then方法