-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #36 from nishimurakaori/ja
translated into Japanese
- Loading branch information
Showing
9 changed files
with
303 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
時折、同じ関数を複数回呼び出したいときがあるでしょう。しかし異なる入力で、**戻り値を考慮せずに** | ||
しかしエラーはチェックしたい。 | ||
|
||
このようなときに`async.each`は便利です。 | ||
|
||
例えば、次のように配列を使って3回コールする例を見てみましょう。 | ||
|
||
```js | ||
var http = require('http') | ||
, async = require('async'); | ||
async.each(['cat', 'meerkat', 'penguin'], function(item, done){ | ||
var opts = { | ||
hostname: 'http://httpbin.org', | ||
path: '/post', | ||
method: 'POST' | ||
}; | ||
var req = http.request(opts, function(res){ | ||
res.on('data', function(chunk){ | ||
}); | ||
res.on('end', function(){ | ||
return done(); | ||
}); | ||
}); | ||
req.write(item); | ||
req.end(); | ||
}, | ||
function(err){ | ||
if (err) console.log(err); | ||
}); | ||
``` | ||
|
||
## チャレンジ | ||
|
||
コマンドラインからfirstとsecondの2つのURLを受け取るプログラムを作成してください。 | ||
その時、`http.get`を使い、URLを受け取るGET requestを作り、エラーがあれば`console.log`に出力してください。 |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
`async.each`を使うと、非同期関数の結果は、 **失われます**。 | ||
|
||
そこで`async.map`を使いましょう。 | ||
これは配列上の非同期イテレータ関数を呼び出して`async.each`と同じことを | ||
しますが、**結果を収集し**コールバックへ渡します。 | ||
|
||
結果は、元の配列のように**同じ順序**である配列です。 | ||
|
||
例えば、それぞれの問題の例は次のように記述することができます: | ||
|
||
```js | ||
var http = require('http') | ||
, async = require('async'); | ||
async.map(['cat', 'meerkat', 'penguin'], function(item, done){ | ||
var opts = { | ||
hostname: 'http://httpbin.org', | ||
path: '/post', | ||
method: 'POST' | ||
}; | ||
var body = ''; | ||
var req = http.request(opts, function(res){ | ||
res.on('data', function(chunk){ | ||
body += chunk.toString(); | ||
}); | ||
res.on('end', function(){ | ||
return done(null, body); | ||
}); | ||
}); | ||
req.write(item); | ||
req.end(); | ||
}, | ||
function(err, results){ | ||
if (err) return console.log(err); | ||
// results is an array of the response bodies in the same order | ||
}); | ||
``` | ||
|
||
## チャレンジ | ||
|
||
コマンドラインから2つのURLを受け取るプログラムを作成してください。 | ||
`http.get`を使い、URLを受け取るGET requestを2つ作成してください。 | ||
`async.map`を使い、`console.log`に結果配列を出力してください。 |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
## チャレンジ | ||
|
||
最初のコマンドライン引数にURLを受け取るプログラムを作成してください。 | ||
|
||
このURLには、以下の配列の値のそれぞれについて、`http.get`を使用してGETリクエストを送信します。 | ||
`number`という名前のクエリパラメータを適切な値に設定してください。 | ||
|
||
```js | ||
['one', 'two', 'three'] | ||
``` | ||
|
||
毎回`Number`をレスポンスボディに変換し、前の値を足します。 | ||
計算結果を`console.log`へ出力してください。 | ||
|
||
## ヒント | ||
|
||
`async.reduce`を使用してください。 | ||
|
||
https://github.com/caolan/async#reduce |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
この問題では、 `async.series`を使用する方法を学習します。 | ||
|
||
`waterfall`と`series`の主な違いは、一度終了した`async.series`タスク関数からの結果は、 | ||
**次に渡されないこと**です。 | ||
` series`は配列としてすべての結果を収集し、**全てのタスクが完了したら**、 | ||
**オプションのコールバック**に渡します。 | ||
例えば: | ||
|
||
```js | ||
async.series([ | ||
function(callback){ | ||
setTimeout(function() { | ||
callback(null, 'one'); | ||
}, 200); | ||
}, | ||
function(callback){ | ||
setTimeout(function() { | ||
callback(null, 'two'); | ||
}, 100); | ||
} | ||
], | ||
// optional callback | ||
function(err, results){ | ||
// results is now equal to ['one', 'two'] | ||
}); | ||
``` | ||
|
||
配列を使う代わりに`async.series`コンテナを使って各プロパティを実行し、 | ||
同じプロパティを持つ結果オブジェクトを作成します。 | ||
上記の例は、以下のように記述できます。 | ||
|
||
```js | ||
async.series({ | ||
one: function(done){ | ||
done(null, '1'); | ||
}, | ||
two: function(done){ | ||
done(null, '2'); | ||
} | ||
}, function(err, results){ | ||
console.log(results); | ||
// results will be {one: 1, two: 2} | ||
}); | ||
``` | ||
|
||
## チャレンジ | ||
|
||
第1、第2引数として2つのURLを受け取るプログラムを書いてください。 | ||
|
||
`http.get`を使い、これらのURLにGETリクエストを作成し、コールバックにレスポンスボディを | ||
渡します。 | ||
|
||
プロパティ名に`requestOne`と`requestTwo`を使用し、`async.series`へ | ||
タスク機能のオブジェクトを渡します。 | ||
|
||
全てのタスクが終了したら、シリーズのコールバック結果を`console.log`へ出力します。 |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# チャレンジ | ||
|
||
コマンドラインから2つの引数(hostname, port)を受け取るプログラムを作成してください。 | ||
`http.request`を使って下記へPOSTしてください。 | ||
|
||
```js | ||
url + '/users/create' | ||
``` | ||
|
||
bodyには`JSON.stringify`を含めてください。 | ||
|
||
```js | ||
{"user_id": 1} | ||
``` | ||
|
||
これを5回繰り返します。`user_id`プロパティは1から始めてインクリメントしてください。 | ||
|
||
リクエスト終了後、GET requestを送ってください: | ||
|
||
```js | ||
url + '/users' | ||
``` | ||
|
||
そしてGET requestのresponse bodyを`console.log`へ出力してください。 | ||
|
||
## ヒント | ||
|
||
この問題では、いくつか非同期操作をコーディネートする必要があります。 | ||
|
||
`async.series`を使用し、`Object`へ渡します。 | ||
タスク機能の一つは`http.request`を`使用してPOSTリクエストを送信するために、 `async.times`を使用する必要があります。 | ||
|
||
`async.times`については下記に詳しく書いてあります。 | ||
|
||
https://github.com/caolan/async#times |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
Node.jsとブラウザで**非同期**JavaScriptを行うには、次の3つの方法があります。 | ||
|
||
最初の方法は、**コールバック地獄**と呼ぶものにつながります。 | ||
コールバック地獄は以下のサイトでのヒントに従うことによって最小限に抑えることができます。 | ||
|
||
http://callbackhell.com. | ||
|
||
別の方法は、 `Promise`パッケージを使用することです。 | ||
`Promise`パッケージを使用すると、コードは簡素化しますが、抽象化のための別のレイヤーを追加します。 | ||
|
||
最後の方法は、Caolan McMahonが作成した `async`パッケージを使用することです。 | ||
`async`パッケージを使用すると、コールバックは書きますが、コールバック地獄に落ちたり、 | ||
`Promise`パッケージを使って抽象化のための別のレイヤーを追加することはありません。 | ||
|
||
多くの場合、先に実行した非同期呼び出しの実行結果によって、 | ||
複数の非同期呼び出しを連続して実行する必要あります。 | ||
|
||
たとえば、次のようなコードです: | ||
|
||
1) 最初のウォーターフォール関数で`http://localhost:3131`をGET request します。 | ||
|
||
2) レスポンスボディは、コールバックを介して次のウォーターフォール関数の引数として渡されます。 | ||
ウォーターフォールの次の機能はパラメーターとしてボディを受け入れ、 | ||
`port`プロパティを取得するため`JSON.parse`し、別のGET requestをします。 | ||
|
||
```js | ||
var http = require('http') | ||
, async = require('async'); | ||
|
||
async.waterfall([ | ||
function(cb){ | ||
var body = ''; | ||
// response is JSON encoded object like the following {port: 3132} | ||
http.get("http://localhost:3131", function(res){ | ||
res.on('data', function(chunk){ | ||
body += chunk.toString(); | ||
}); | ||
res.on('end', function(){ | ||
cb(null, body); | ||
}); | ||
}).on('error', function(err) { | ||
cb(err); | ||
}); | ||
}, | ||
|
||
function(body, cb){ | ||
var port = JSON.parse(body).port; | ||
var body = ''; | ||
http.get("http://localhost:" + port, function(res){ | ||
res.on('data', function(chunk){ | ||
body += chunk.toString(); | ||
}); | ||
res.on('end', function(){ | ||
cb(null, body); | ||
}); | ||
}).on('error', function(err) { | ||
cb(err); | ||
}); | ||
} | ||
], function(err, result){ | ||
if (err) return console.error(err); | ||
console.log(result); | ||
}); | ||
``` | ||
|
||
## チャレンジ | ||
|
||
この問題では、最初のファイルの内容を読み取るプログラムを作成する必要があります。 | ||
|
||
パスはプログラムの最初のコマンドライン引数として提供されます | ||
(`process.argv [ 2 ]` ) 。 | ||
|
||
ファイルには、単一のURLが含まれています。 | ||
`http.get`を使用して、このURLにGETリクエストを作成し、 レスポンスボディを`console.log`へ出力します 。 |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
## チャレンジ | ||
|
||
コマンドライン引数にURLを1つ受け取るプログラムを作成してください。 | ||
|
||
`async.whilst`と`http.get`を使用して、レスポンスボディは、文字列`"meerkat"`を | ||
含むまでこのURLにGETリクエストを送信します。 | ||
|
||
"meerkat"の文字列を取得するために必要なGET Requestの量を`console.log`へ出力してください。 | ||
|
||
## ヒント | ||
|
||
`String.prototype.trim()`が力になるでしょう。 | ||
|
||
`async.whilst()`のドキュメントはこちらにあります: | ||
|
||
https://github.com/caolan/async#whilst |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{yellow}{bold}{appname}の演習でトラブルが起きましたか?{/bold}{/yellow} | ||
|
||
Node.jsの一般的なヘルプを探しているなら、Freenode IRCの | ||
#Node.jsチャネルで誰かが助けてくれるでしょう。 | ||
Node.jsのGoogle Groupもあります。 | ||
|
||
https://groups.google.com/forum/#!forum/nodejs | ||
|
||
{yellow}{bold}{appname}のバグを見つけたり、貢献したい時には?{/bold}{/yellow} | ||
|
||
{appname}のオフィシャルリポジトリはこちらです: | ||
https://github.com/bulkan/async-you/ | ||
バグレポートや、改善のプルリクエストはお気軽にどうぞ。 |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{ | ||
"title": "async you - learn to use the async package", | ||
"subtitle": "\u001b[23mSelect an exercise and hit \u001b[3mEnter\u001b[23m to begin", | ||
"exercise": { | ||
"WATERFALL": "WATERFALL", | ||
"SERIES OBJECT": "SERIES OBJECT", | ||
"EACH": "EACH", | ||
"MAP": "MAP", | ||
"TIMES": "TIMES", | ||
"REDUCE": "REDUCE", | ||
"WHILST": "WHILST" | ||
} | ||
} |