Skip to content

Instantly share code, notes, and snippets.

@butackle
Created January 14, 2017 07:51
Show Gist options
  • Save butackle/3ebb36f3a74de17121ef9b0adbfde462 to your computer and use it in GitHub Desktop.
Save butackle/3ebb36f3a74de17121ef9b0adbfde462 to your computer and use it in GitHub Desktop.
const testPromise = (num,sec) => {
const random = Math.round(Math.random() * 10); //ランダムで0〜9の数値を返す
return new Promise((resolve, reject) => {
setTimeout(() => {
console.log(num + ": random is " + random);
if (random === 3) reject("random is three");
resolve("random is " + random)
},sec);
})
}
const p1 = testPromise(1, 3000);
const p2 = console.log("Not Promise Method");
const p3 = testPromise(2,10);
const p4 = testPromise(3,2);
Promise.all([p1, p2, p3, p4])
.then((results) => results.forEach((result) => console.log("Success: " + result)))
.catch((err) => console.log("Error: " + err))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment