Closed sounisi5011 closed 5 years ago
unifiedのTransformerの第三引数next
はどうすれば良いんだろう。
If the signature of a transformer includes
next
(third argument), the function may finish asynchronous, and must invokenext()
.
この引数が渡されたら、next
関数を呼び出さないといけないってこと…?
unifiedのTransformerの第三引数
next
はどうすれば良いんだろう。If the signature of a transformer includes
next
(third argument), the function may finish asynchronous, and must invokenext()
.この引数が渡されたら、
next
関数を呼び出さないといけないってこと…?
コードをたどったら判明した。Transformer関数内で非同期処理をする場合は、
// Promise<Unist.Node>を返す
function transformer(node, file) {
return new Promise((resolve, reject) => {
node2node(node, file, (error, resultNode) => {
if (error) {
reject(error);
} else {
resolve(resultNode);
}
});
});
}
// 第三引数 next コールバック関数を使用する
function transformer(node, file, next) {
node2node(node, file, (error, resultNode) => {
next(error || null, resultNode, file);
});
}
このどちらの書き方もできるらしい。
unifiedが使用しているtroughは、Transformer関数の引数の数でこの判定を行っているみたいだ。
つまり、Promiseスタイルでやっている限り、第三引数next
を関数の定義に含める必要は無い。
buildしたものにhast.d.ts
が含まれていない…
含むようにしたほうが良いんだろうか…?
エラー時の処理等が気になるけれど、とりあえずいったん終わりにしよう。
unistのような、有志が規格化したASTを導入する。 https://github.com/sounisi5011/vec-draw/issues/15 で提案された作業を試みる。