teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

1

質問内容の修正

2020/06/04 02:37

投稿

Radec
Radec

スコア21

title CHANGED
@@ -1,1 +1,1 @@
1
- オライリーのサンプルにPromise導入たい
1
+ オライリーのサンプルでresolve使うと処理が停止てしまう
body CHANGED
@@ -1,49 +1,71 @@
1
- Node.js デザインパターン第2版のサンプルプログラム(https://github.com/mushahiroyuki/ndp2)Chaptar4の11_generators_async_flow_thunksというサンプルがあります。
1
+ Node.js デザインパターン第2版のサンプルプログラム(https://github.com/mushahiroyuki/ndp2)Chaptar4の10_generators_async_flowというサンプルがあります。
2
+ これは、generatorを使ってあるファイルのコピーを作成するプログラムです。
2
3
 
4
+ 元コード
5
+
3
6
  ```JavaScript
4
7
  "use strict";
5
8
 
6
9
  const fs = require('fs');
7
10
  const path = require('path');
8
11
 
9
- function asyncFlowWithThunks(generatorFunction) {
12
+ function asyncFlow(generatorFunction) {
10
13
  function callback(err) {
11
- if (err) {
14
+ if (err) {
12
15
  return generator.throw(err);
13
16
  }
14
- console.log(arguments);
15
17
  const results = [].slice.call(arguments, 1);
16
- const thunk = generator.next(results.length > 1 ? results : results[0]).value;
18
+ generator.next(results.length > 1 ? results : results[0]);
17
- console.log(thunk.toString());
18
- thunk && thunk(callback);
19
19
  }
20
- const generator = generatorFunction();
20
+ const generator = generatorFunction(callback);
21
- const thunk = generator.next().value;
21
+ generator.next();
22
- console.log(thunk.toString());
23
- thunk && thunk(callback);
24
22
  }
25
23
 
26
- const readFileThunk = (filename, options) => {
27
- return (cb) => {
28
- fs.readFile(filename, options, cb);
29
- }
30
- };
31
-
32
- const writeFileThunk = (filename, options) => {
33
- return (cb) => {
34
- fs.writeFile(filename, options, cb);
35
- }
36
- };
37
-
38
- asyncFlowWithThunks(function* () {
24
+ asyncFlow(function* (callback) {
39
25
  const fileName = path.basename(__filename);
40
- const myself = yield readFileThunk(fileName, 'utf8');
26
+ const myself = yield fs.readFile(fileName, 'utf8', callback);
41
- yield writeFileThunk(`clone_of_${fileName}`, myself);
27
+ yield fs.writeFile(`clone_of_${fileName}`, myself, callback);
42
28
  console.log('Clone created');
43
29
  });
44
-
45
30
  ```
46
31
 
47
- 応用課題として、yieldble な Promise を使ってこれを書き換えるというものが提示されていますが、
32
+ 応用課題として、yieldable な Promise を使ってこれを書き換えるというものが提示されています
33
+
34
+ 以下の通りに実装してみたものの、記載のconsle.log以降の処理が実行されず正常に動きません。
48
- Promiseどこに導入すべきか全く見もつきません
35
+ (generator.next与えと、然問題なく動きま
36
+
37
+ 私の理解では、yieldableなPromiseであれば、resolveで後続処理をキックできるものかと認識しておりましたが、
38
+ どのような原因で後続処理を動かすことができていないものでしょうか?
39
+
40
+ ```JavaScript
41
+ "use strict";
42
+ const fs = require('fs');
43
+ const path = require('path');
44
+
45
+ function asyncFlow(generatorFunction) {
46
+ function callback(err) {
49
- これといった正解は無いと思いますが、どこで new Promise するか、皆さんのアイディアを頂きたいです。
47
+ return new Promise((resolve, reject) => {
48
+ if (err) {
49
+ reject(generator.throw(err));
50
+ }
51
+ const results = [].slice.call(arguments, 1);
52
+ resolve(results.length > 1 ? results : results[0]); //ここをresolve(generator.next(results.length > 1 ? results : results[0])); に修正すると動く
53
+ }
54
+ );
55
+ }
56
+ const generator = generatorFunction(callback);
57
+ generator.next();
58
+ }
59
+
60
+ asyncFlow(function* (callback) {
61
+ const fileName = path.basename(__filename);
62
+ const myself = yield fs.readFile(fileName, 'utf8', callback);
63
+ console.log('----ここに戻って来れない----');
64
+ const err = yield fs.writeFile(`clone_of_${fileName}`, myself, callback);
65
+ if (err) {
66
+ console.log('Error');
67
+ } else {
68
+ console.log('Clone created');
69
+ }
70
+ });
71
+ ```