我在SO中发现了下面的代码,为了更好地理解promise的概念,我尝试调试它,目前有一些我在下面的代码中不理解,这就是为什么最后得到的值(在特定的Twitter)与id == 4,而不是所有的推文列表?承诺链如何工作?
$.get('profile.json').then(function (profile) {
return $.get('tweets.json').then(function (response) {
return response.filter(function (tweet) {
//this is return that not related to the promise just to the filter
return tweet.id === 4;
});
});
}).then(function (specificTweet) {
...
这是JSON文件,
[
{
"id": 1,
"tweet": "OMG, worst day ever, my BF @BobbyBoo dumped me",
"usersMentioned": [
{
"id": 10,
"username": "BobbyBoo"
}
]
},
{
"id": 2,
"tweet": "OMG, best day ever, my BF came back to me"
},
{
"id": 3,
"tweet": "OMG, worst day ever, just don't ask"
},
{
"id": 4,
"tweet": "@BobbyBoo OMG...just OMG!",
"usersMentioned": [
{
"id": 10,
"username": "BobbyBoo"
}
]
}
]