Skip to content

Instantly share code, notes, and snippets.

@Kuniwak
Created July 1, 2013 11:44
Show Gist options
  • Save Kuniwak/5900152 to your computer and use it in GitHub Desktop.
Save Kuniwak/5900152 to your computer and use it in GitHub Desktop.
Promiseのサンプルコード。
// 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