Last active
February 5, 2018 01:51
-
-
Save ichiroku11/f6603334a0df984c9a1c074d9adaf0b0 to your computer and use it in GitHub Desktop.
async/awaitとPromiseを試す
This file contains hidden or 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とPromiseのサンプル | |
| // Promiseを返す関数 | |
| const actionAsync = (result: number) => { | |
| const promise = new Promise<number>((resolve, reject) => { | |
| // 1秒後にresolveする | |
| setTimeout(() => resolve(result), 1000); | |
| }); | |
| return promise; | |
| }; | |
| // テスト用の関数 | |
| const testAcync = async () => { | |
| console.log("await1"); | |
| const result1 = await actionAsync(1); | |
| console.log(`result1: ${result1}`); | |
| console.log("await2"); | |
| const result2 = await actionAsync(2); | |
| console.log(`result2: ${result2}`); | |
| }; | |
| testAcync(); | |
| /* | |
| await1 | |
| result1: 1 | |
| await2 | |
| result2: 2 | |
| */ |
This file contains hidden or 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とPromise | |
| // rejectした場合 | |
| // Promiseを返す関数 | |
| const actionAsync = (result: number) => { | |
| const promise = new Promise<number>((resolve, reject) => { | |
| // 1秒後にresolveまたはrejectする | |
| setTimeout(() => result < 0 ? reject("エラー") : resolve(result), 1000); | |
| }); | |
| return promise; | |
| }; | |
| // テスト用の関数 | |
| const testAcync = async () => { | |
| try { | |
| console.log("await1"); | |
| const result1 = await actionAsync(1); | |
| console.log(`result1: ${result1}`); | |
| console.log("await2"); | |
| const result2 = await actionAsync(-1); | |
| console.log(`result2: ${result2}`); | |
| } catch (error) { | |
| console.log(`error: ${error}`); | |
| } | |
| }; | |
| testAcync(); | |
| /* | |
| await1 | |
| result1: 1 | |
| await2 | |
| error: エラー | |
| */ |
This file contains hidden or 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を返す | |
| // https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Statements/async_function | |
| // 値を返すとresolveしたPromiseを返す | |
| const resolveAsync = async (result: number) => result; | |
| // 例外や値をスローした場合はrejectされたPromiseを返す | |
| const rejectAsync = async (message: string) => { | |
| throw message; | |
| } | |
| // テスト用の関数 | |
| const testAcync = async () => { | |
| try { | |
| console.log("await1"); | |
| const result1 = await resolveAsync(1); | |
| console.log(`result1: ${result1}`); | |
| console.log("await2"); | |
| const result2 = await rejectAsync("エラー"); | |
| console.log(`result2: ${result2}`); | |
| } catch (error) { | |
| console.log(`error: ${error}`); | |
| } | |
| }; | |
| testAcync(); | |
| /* | |
| await1 | |
| result1: 1 | |
| await2 | |
| error: エラー | |
| */ |
ichiroku11
commented
Jan 31, 2018
Author
- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function
- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/await
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment