Created
July 1, 2013 11:44
-
-
Save Kuniwak/5900152 to your computer and use it in GitHub Desktop.
Promiseのサンプルコード。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// kriszyp/node-promiseを使うので、スクリプトのある場所で下のコマンドを実行してくだし。 | |
// $ npm install node-promise | |
var Promise = require('node-promise').Promise; | |
getAccessToken().then(function(token) { | |
return getImage(token); | |
}).then(function(image) { | |
// Processing image | |
console.log(image); | |
}); | |
// Tokenを取得するpromiseを返す。 | |
function getAccessToken() { | |
var promise = new Promise(); | |
setTimeout(function() { | |
promise.resolve('TOKEN'); | |
}, 1000); | |
return promise; | |
} | |
// Imageを取得するpromiseを返す。 | |
function getImage(token) { | |
var promise = new Promise(); | |
setTimeout(function() { | |
promise.resolve(token + '::IMAGE'); | |
}, 1000); | |
return promise; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment