Last active
February 12, 2018 05:34
-
-
Save ichiroku11/738c8133e5f4e9747979fa86dc337515 to your computer and use it in GitHub Desktop.
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
| // 指定したミリ秒後にresolveするPromiseを返す | |
| function resolveAsync<TValue>(value: TValue, timeout: number): Promise<TValue> { | |
| return new Promise<TValue>((resolve, reject) => { | |
| setTimeout(() => { | |
| console.timeStamp(`resolve: ${value}`); | |
| resolve(value); | |
| }, timeout); | |
| }); | |
| } | |
| // 指定したミリ秒後にrejectするPromiseを返す | |
| function rejectAsync<TError>(error: TError, timeout: number): Promise<never> { | |
| return new Promise<never>((resolve, reject) => { | |
| setTimeout(() => { | |
| console.timeStamp(`reject: ${error}`); | |
| reject(error); | |
| }, timeout); | |
| }); | |
| } |
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
| // Promise.allはすべてがresolveするとresolveするか、1つでもrejectしたらrejectするPromiseを返す | |
| // resolveする場合 | |
| async function allTestAsync(): Promise<void> { | |
| // 3秒後にすべてがresolve | |
| const result = await Promise.all([ | |
| resolveAsync("a", 3000), | |
| resolveAsync("b", 2000), | |
| resolveAsync("c", 1000), | |
| ]); | |
| console.timeStamp(`result: [${result}]`); | |
| // result: [a, b, c] | |
| }; | |
| console.timeStamp(`start`); | |
| allTestAsync(); | |
| console.timeStamp(`end`); | |
| // 実行結果 | |
| /* | |
| start: 166.396 ミリ秒 | |
| end: 166.756 ミリ秒 | |
| resolve: c: 1,171.296 ミリ秒 | |
| resolve: b: 2,181.357 ミリ秒 | |
| resolve: a: 3,197.177 ミリ秒 | |
| result: [a,b,c]: 3,198.277 ミリ秒 | |
| */ |
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
| // Promise.allはすべてがresolveするとresolveするか、1つでもrejectしたらrejectするPromiseを返す | |
| // rejectする場合 | |
| async function allTest2Async(): Promise<void> { | |
| try { | |
| // 1秒後に"エラー2"がreject | |
| const result = await Promise.all([ | |
| resolveAsync("a", 3000), | |
| rejectAsync("エラー1", 2000), | |
| rejectAsync("エラー2", 1000), | |
| ]); | |
| // エラーになるのでここには到達しない | |
| console.timeStamp(`result: [${result}]`); | |
| } catch (error) { | |
| console.timeStamp(`error: ${error}`); | |
| // error: エラー2 | |
| } | |
| }; | |
| console.timeStamp(`start`); | |
| allTest2Async(); | |
| console.timeStamp(`end`); | |
| // 実行結果 | |
| /* | |
| start: 119.374 ミリ秒 | |
| end: 120.994 ミリ秒 | |
| reject: エラー2: 1,133.014 ミリ秒 | |
| error: エラー2: 1,133.814 ミリ秒 | |
| reject: エラー1: 2,135.434 ミリ秒 | |
| resolve: a: 3,121.634 ミリ秒 | |
| */ |
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
| // Promise.raceはどれか1つがresolveしたらresolveするか、どれか1つがrejectしたらrejectするPromiseを返す | |
| // resolveする場合 | |
| async function raceTestAsync(): Promise<void> { | |
| // 1秒後に"c"がresolve | |
| const result = await Promise.race([ | |
| resolveAsync("a", 3000), | |
| resolveAsync("b", 2000), | |
| resolveAsync("c", 1000), | |
| ]); | |
| console.timeStamp(`result: ${result}`); | |
| // result: c | |
| }; | |
| console.timeStamp(`start`); | |
| raceTestAsync(); | |
| console.timeStamp(`end`); | |
| // 実行結果 | |
| /* | |
| start: 177.959 ミリ秒 | |
| end: 178.599 ミリ秒 | |
| resolve: c: 1,188.699 ミリ秒 | |
| result: c: 1,190.079 ミリ秒 | |
| resolve: b: 2,191.279 ミリ秒 | |
| resolve: a: 3,195.899 ミリ秒 | |
| */ |
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
| // Promise.raceはどれか1つがresolveしたらresolveするか、どれか1つがrejectしたらrejectするPromiseを返す | |
| // rejectする場合 | |
| async function raceTest2Async(): Promise<void> { | |
| try { | |
| // 1秒後に"エラー2"がreject | |
| const result = await Promise.race([ | |
| resolveAsync("a", 3000), | |
| rejectAsync("エラー1", 2000), | |
| rejectAsync("エラー2", 1000), | |
| ]); | |
| // エラーになるのでここには到達しない | |
| console.timeStamp(`result: ${result}`); | |
| } catch (error) { | |
| console.timeStamp(`error: ${error}`); | |
| // error: エラー2 | |
| } | |
| }; | |
| console.timeStamp(`start`); | |
| raceTest2Async(); | |
| console.timeStamp(`end`); | |
| // 実行結果 | |
| /* | |
| start: 206.644 ミリ秒 | |
| end: 207.464 ミリ秒 | |
| reject: エラー2: 1,224.344 ミリ秒 | |
| error: エラー2: 1,225.584 ミリ秒 | |
| reject: エラー1: 2,224.184 ミリ秒 | |
| resolve: a: 3,224.004 ミリ秒 | |
| */ |
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
| // Promise.rejectはrejectしたPromiseを返す | |
| async function rejectTestAcync(): Promise<void> { | |
| try { | |
| // rejectはPromise<never>型を返す | |
| await Promise.reject(`エラー`); | |
| } catch (error) { | |
| // rejectに渡した引数が例外としてスローされる | |
| console.log(`error: ${error}`); | |
| } | |
| }; | |
| console.log(`start`); | |
| rejectTestAcync(); | |
| console.log(`end`); | |
| // 実行結果 | |
| /* | |
| start | |
| end | |
| 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
| // Promise.resolveはresolveした(完了した)Promiseを返す | |
| async function resolveTestAcync(): Promise<void> { | |
| // resolveはPromise<T>を返す | |
| // awaitで一旦関数を抜ける | |
| const result = await Promise.resolve(1); | |
| console.log(`result: ${result}`); | |
| }; | |
| console.log(`start`); | |
| resolveTestAcync(); | |
| console.log(`end`); | |
| // 実行結果 | |
| /* | |
| start | |
| end | |
| result: 1 | |
| */ |
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
Show hidden characters
| { | |
| "compileOnSave": true, | |
| "compilerOptions": { | |
| "noImplicitAny": false, | |
| "noEmitOnError": true, | |
| "removeComments": false, | |
| "sourceMap": true, | |
| // JavaScriptのasync/awaitを使うため | |
| "target": "es2017", | |
| "module": "es2015", | |
| "moduleResolution": "node", | |
| "lib": [ | |
| "es5", | |
| "es2015", | |
| "dom" | |
| ] | |
| }, | |
| "exclude": [ | |
| "node_modules", | |
| "wwwroot" | |
| ] | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment