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
function sumOfArray(arr, sum = 0){ | |
if(arr.length === 0) return sum; | |
else{ | |
const [first, ...rest] = arr; | |
return sumOfArray(rest, first + sum); | |
} | |
} |
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
/** | |
* async/awaitを用いて並列処理 | |
*/ | |
//sec秒待ってresolveする | |
function sample(number, sec){ | |
return new Promise((resolve, reject)=>{ | |
setTimeout(()=>{ | |
console.log("number=" + number + " sec=" + sec); | |
resolve(number); |
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
/** | |
* 1秒ごとに1文字ずつ文字を出力してみる | |
*/ | |
const word = "javascript"; | |
function outputPerSec(char){ | |
return new Promise((resolve, reject)=>{ | |
setTimeout(()=>{ | |
resolve(char); |
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
/** | |
* awaitの簡単な使い方 | |
*/ | |
//Promiseをreturnして、非同期処理を内部で行う(普通の)関数 | |
function sample1(value){ | |
return new Promise((resolve, reject)=>{ | |
setTimeout(()=>{ | |
resolve(value); |
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
/** | |
* async関数の戻り値がPromiseであることを確認 | |
*/ | |
//パターン1 | |
//thenに処理を繋げる | |
async function sample1(){ | |
return "SUCCESS!"; | |
} |
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
/** | |
* asyncの基本的な使い方 | |
*/ | |
//静的なfunction文 | |
async function sample1(){ | |
//何かしらの処理 | |
} | |
//代入でも使える |
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
/** | |
* Promise.allの基本的な使い方 | |
* | |
* 逐次処理と処理の違いを比較してみてください。 | |
* | |
*/ | |
const promise1 = new Promise((resolve, reject)=>{ | |
setTimeout(()=>{ | |
console.log("promise1"); |
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
/** | |
* thenの戻り値を、以下の2通りで確認してみる。 | |
* | |
* 1. thenが普通の値をreturnするとき。 | |
* 2. thenがPromiseをreturnするとき | |
* | |
*/ | |
//1. thenが普通の値をreturnするとき。 | |
new Promise((resolve, reject)=>{ |
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
/** | |
* Promiseの基本的な使い方 | |
* | |
* new Promise((resolve, reject)=>{ | |
* 何かしら非同期処理 | |
* ・・・・ | |
* ・・・・ | |
* | |
* if(成功!) resolve(value); | |
* if(失敗!) reject(error); |
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
/** | |
* 1秒待ってから"First"と表示し、その直後に"Second"と表示したいと思っている | |
* (以下のコードでは逆になってしまいます。うまくない) | |
*/ | |
setTimeout(()=>{ | |
console.log("First"); | |
}, 1000); | |
console.log("Second"); |