Last active
August 29, 2015 14:07
-
-
Save tyage/8173d556ebbbf3e79ec1 to your computer and use it in GitHub Desktop.
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
// n秒経ったらresolve | |
var delay = function(second) { | |
return new Promise(function(resolve, reject) { | |
setTimeout(function() { | |
resolve(second) | |
}, 1000 * second) | |
}) | |
} | |
delay(2).then(function(n) { | |
console.log(n) | |
}) | |
// Promise#catch | |
new Promise(function() { | |
throw new Error(1) | |
}).catch(function(a) { | |
console.log(a) | |
}) | |
new Promise(function(resolve, reject) { | |
reject(2) | |
}).catch(function(a) { | |
console.log(a) | |
}) | |
// resolveにpromiseを渡すとrejectされる (Firefox 32だと正しく動作しない) | |
(function() { | |
var p = new Promise(function(resolve, reject) { | |
setTimeout(function() { | |
console.log(p) | |
resolve(p) | |
}) | |
}) | |
p.then(function() { | |
console.log('resolved') | |
}, function() { | |
console.log('rejected') | |
}) | |
})() | |
// Promise.all | |
Promise.all([delay(2), delay(3)]).then(function(n) { | |
console.log(n) | |
}) | |
// Promise.race | |
Promise.race([delay(2), delay(3)]).then(function(n) { | |
console.log(n) | |
}) | |
// Promise.reject | |
Promise.reject(delay(2)).then(function() { | |
console.log('resolved') | |
}, function() { | |
console.log('rejected') | |
}) | |
// Promise.resolve | |
Promise.resolve(delay(2)).then(function() { | |
console.log('resolved') | |
}, function() { | |
console.log('rejected') | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment