Showing
5 changed files
with
103 additions
and
0 deletions
09_JavaScript/.jshintrc
0 → 100644
09_JavaScript/01_hello.js
0 → 100644
09_JavaScript/02_promise.js
0 → 100644
| 1 | +"use strict"; | ||
| 2 | + | ||
| 3 | +function asyncTask1(cb) { | ||
| 4 | + console.log("task1 start"); | ||
| 5 | + setTimeout(function() { | ||
| 6 | + cb("task1"); | ||
| 7 | + }, 2000); | ||
| 8 | +} | ||
| 9 | + | ||
| 10 | +function asyncTask2(cb) { | ||
| 11 | + console.log("task2 start"); | ||
| 12 | + setTimeout(function() { | ||
| 13 | + cb("task2"); | ||
| 14 | + }, 1000); | ||
| 15 | +} | ||
| 16 | + | ||
| 17 | +asyncTask2(function(ret) { | ||
| 18 | + console.log(ret + " done"); | ||
| 19 | + asyncTask1(function(ret2) { | ||
| 20 | + console.log(ret2 + "done"); | ||
| 21 | + // 추가적인 callback이 더 생긴다면??? | ||
| 22 | + }); | ||
| 23 | +}); | ||
| 24 | + | ||
| 25 | +function promiseTask1() { | ||
| 26 | + console.log("task1 start"); | ||
| 27 | + return new Promise(function(resolve, reject) { | ||
| 28 | + setTimeout(function() { | ||
| 29 | + resolve('task1'); | ||
| 30 | + }, 2000); | ||
| 31 | + }); | ||
| 32 | +} | ||
| 33 | + | ||
| 34 | +function promiseTask2() { | ||
| 35 | + console.log("task2 start"); | ||
| 36 | + return new Promise(function(resolve, reject) { | ||
| 37 | + setTimeout(function() { | ||
| 38 | + resolve('task2'); | ||
| 39 | + }, 1000); | ||
| 40 | + }); | ||
| 41 | +} | ||
| 42 | + | ||
| 43 | +promiseTask2() | ||
| 44 | +.then(function(ret) { | ||
| 45 | + console.log(ret + " done"); | ||
| 46 | +}).then(function() { | ||
| 47 | + return promiseTask1(); | ||
| 48 | +}) | ||
| 49 | +.then(function(ret) { | ||
| 50 | + console.log(ret + " done"); | ||
| 51 | +}); |
09_JavaScript/03_sort.js
0 → 100644
| 1 | +"use strict"; | ||
| 2 | + | ||
| 3 | +function swap(arr, i, j) { | ||
| 4 | + var t = arr[i]; | ||
| 5 | + arr[i] = arr[j]; | ||
| 6 | + arr[j] = t; | ||
| 7 | +} | ||
| 8 | + | ||
| 9 | +function bubbleSort(arr) { | ||
| 10 | + var i, j; | ||
| 11 | + for (i = 0; i < arr.length; i++) { | ||
| 12 | + for (j = 0; j < arr.length - i; j++) { | ||
| 13 | + if (arr[j] > arr[j+1]) { | ||
| 14 | + swap(arr, j, j+1); | ||
| 15 | + } | ||
| 16 | + } | ||
| 17 | + } | ||
| 18 | + return arr; | ||
| 19 | +} | ||
| 20 | + | ||
| 21 | +var arr1 = [6, 3, 2, 7, 8, 3, 1, 0, 9, 5]; | ||
| 22 | +var arr2 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; | ||
| 23 | +var arr3 = [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]; | ||
| 24 | + | ||
| 25 | +console.log(bubbleSort(arr1)); | ||
| 26 | +console.log(bubbleSort(arr2)); | ||
| 27 | +console.log(bubbleSort(arr3)); | ||
| ... | \ No newline at end of file | ... | \ No newline at end of file |
-
Please register or login to post a comment