Callback
Example
function Add( num, callback ) {
num = num + 1;
callback( num );
}
Add( 2, function( ans ) {
console.log( ans );
});
Why we need it?
- 為了防止資料還沒收到就結束程式或回傳參數
- 最常使用的地方
[
{
"id": 1,
"title": "post 1"
},
{
"id": 2,
"title": "post 2"
},
{
"id": 3,
"title": "post 3"
}
]
{
"id": 1,
"authorId": 2,
"content": "今天天氣真好"
}
{
"id": 2,
"name": "Deleav",
"email": "[email protected]"
}
AJAX function
function getArticleList( callback ) {
$.ajax({
url: "/api/article",
type: "get"
}).done(function( articleList ) {
callback( articleList );
});
}
function getArticle( id, callback ) {
$.ajax({
url: "/api/articleInfo" + id,
type: "get"
}).done(function( article ) {
callback( article );
});
}
function getAuthor( authorId, callback ) {
$.ajax({
url: "/api/author/" + authorId,
type: "get"
}).done(function( authorInfo ){
callback( authorInfo );
});
}
取得文章列表
function getArticleList( callback ) {
$.ajax({
url: "/api/article",
type: "get"
}).done(function( articleList ) {
callback( articleList );
});
}
getArticleList(function( articleList ) {
console.log( articleList );
});
[
{
"id": 1,
"title": "post 1"
},
{
"id": 2,
"title": "post 2"
},
{
"id": 3,
"title": "post 3"
}
]
取得第一篇文章的內容
function getArticleList( callback ) {
$.ajax({
url: "/api/article",
type: "get"
}).done(function( articleList ) {
callback( articleList );
});
}
function getArticle( id, callback ) {
$.ajax({
url: "/api/articleInfo/" + id,
type: "get"
}).done(function( article ) {
callback( article );
});
}
getArticleList(function( articleList ) {
getArticle(articleList[0].id, function( articleInfo ) {
console.log( articleInfo );
});
});
{
"id": 1,
"authorId": 2,
"content": "今天天氣真好"
}
取得第一篇文章的作者資訊
getArticleList(function( articleList ) {
getArticle(articleList[0].id, function( articleInfo ) {
getAuthor(articleInfo.arthorId, function( arthorInfo ) {
console.log( arthorInfo );
});
});
});
{
"id": 2,
"name": "Deleav",
"email": "[email protected]"
}
Promise
- 在 return 值外面包一層 Promise
- Promise 是個物件,有許多的 method 能夠使用( ex: then, catch...等 )
function getArticleList( callback ) {
return new Promise(function( resolve, reject ) {
$.ajax({
url: "/article",
type: "get"
}).done(function( articleList ) {
return resolve( articleList );
});
}
}
function getArticle( id, callback ) {
return new Promise(function( resolve, reject ) {
$.ajax({
url: "/articleInfo/" + id,
type: "get"
}).done(function( article ) {
return resolve( article );
});
}
}
function getAuthor( authorId, callback ) {
return new Promise(function( resolve, reject ) {
$.ajax({
url: "/author/" + authorId,
type: "post"
}).done(function( authorInfo ){
return resolve( authorInfo );
});
}
}
getArticleList(function( articleList ) {
getArticle(articleList[0].id, function( articleInfo ) {
getAuthor(articleInfo.arthorId, function( arthorInfo ) {
console.log( arthorInfo );
});
});
});
getArticleList().then(function( articleList ) {
return getArticle( articleList[0].id );
}).then(function( article ) {
return getArticle( article.authorId );
}).then(function( authorInfo ) {
console.log( authorInfo );
});
{
"id": 2,
"name": "Deleav",
"email": "[email protected]"
}
Arrow function
getArticleList().then(function( articleList ) {
return getArticle( articleList[0].id );
}).then(function( article ) {
return getArticle( article.authorId );
}).then(function( authorInfo ) {
console.log( authorInfo );
});
getArticleList()
.then( articleList => getArticle( articleList[0].id ))
.then( article => getArticle( article.authorId ))
.then( authorInfo => {
console.log( authorInfo )
});
Async and await
- 一定要用 Promise 包起來才能夠使用
- 想要用
await
一定要加 async
async function() {
var articleList = await getArticleList();
var article = await getArticle(articleList[0].id);
var authorInfo = await getAuthor(article.authorId);
console.log( arthorInfo );
}
async () => {
var articleList = await getArticleList();
var article = await getArticle(articleList[0].id);
var authorInfo = await getAuthor(article.authorId);
console.log( arthorInfo );
}
DEMO