promise with async
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<script src="angular.js"> </script>
</head>
<body ng-app="app" ng-controller="con">
</body>
<script>
var app=angular.module("app",[]);
app.controller("con",function($scope,$http,$q){
function asyncGreet(name) {
var deferred = $q.defer();
setTimeout(function() {
deferred.resolve('Hello, ' + name + '!');
}, 1000);
return deferred.promise;
}
function getjson () {
//return $http.get("x.json");
var url="http://jsonp.nodejitsu.com/?url=http://api.fanfou.com/users/show/wangxing.json"
return $http.get(url);
}
var promise = asyncGreet('Robin Hood');
promise.then(function(greeting) {
console.log('Success1: ' + greeting);
return getjson();
}).then(function(greeting) {
console.log('Success2: ' + greeting);
return asyncGreet('Robin Hood 3');
}).then(function(greeting) {
console.log('Success3: ' + greeting);
//return asyncGreet('Robin Hood 4');
return getjson();
}).then(function(greeting) {
console.log('Success4: ' + greeting);
});
})
</script>
</html>
相關文章
- Async & generator & PromisePromise
- [Javascript] Promise question with async awaitJavaScriptPromiseAI
- async/await 和 promise/promise.all 的示例AIPromise
- Async/Await 代替 Promise.all()AIPromise
- Promise與async/await與GeneratorPromiseAI
- Promise和async await詳解PromiseAI
- Promise/async/await 研究筆記PromiseAI筆記
- 重構:從Promise到Async/AwaitPromiseAI
- Promise && async/await的理解和用法PromiseAI
- Promise、Generator、Async有什麼區別?Promise
- 單例模式,promise與async/await單例模式PromiseAI
- Async/Await替代Promise的6個理由AIPromise
- 簡單聊聊ES6-Promise和AsyncPromise
- 面試向:Async/Await 代替 Promise.all()面試AIPromise
- promise以及async、await學習總結PromiseAI
- 理解koa2 之 async + await + promiseAIPromise
- Promise, Generator, async/await的漸進理解PromiseAI
- [面試專題]JS非同步之Promise,Generator,Async面試JS非同步Promise
- async await函式效能與Promise併發AI函式Promise
- 與Promise血脈相連的async/awaitPromiseAI
- async await、Promise、setTimeout執行順序AIPromise
- ES6 Promise 和 Async/await的使用PromiseAI
- 深入理解 promise、generator+co、async/await 用法PromiseAI
- 多執行緒07:async、future、packaged_task、promise執行緒PackagePromise
- 回撥函式到promise再到理解async/await函式PromiseAI
- 理解非同步之美:Promise 與 async await(二)非同步PromiseAI
- 理解非同步之美:Promise與async await(一)非同步PromiseAI
- setTimeout&Promise&Async之間的愛恨情仇Promise
- Promise(es6)和await,async(es7)PromiseAI
- JavaScript學習筆記(一) promise和async/waitJavaScript筆記PromiseAI
- 細說 async/await 相較於 Promise 的優勢AIPromise
- JS非同步之callback、promise、async+await簡介JS非同步PromiseAI
- 20分鐘帶你掌握JavaScript Promise和 Async/AwaitJavaScriptPromiseAI
- 結合async await,動態設定Promise.all()AIPromise
- promise、async、await非同步原理與執行順序PromiseAI非同步
- JS非同步程式設計 (2) – Promise、Generator、async/awaitJS非同步程式設計PromiseAI
- node傳統讀取檔案和promise,async await,PromiseAI
- JavaScript 的 Async/Await 完勝 Promise 的六個理由JavaScriptAIPromise