これはあまりにも漠然としていますが、約束事ではなくES6ジェネレータ機能を使用する利点は何ですか?私は現時点で優位性を見ておらず、誰かがそれに光を当てることを望んでいた。例えばES6 Generator Function vs Promises
、非同期にデータを取得する:発電機は、自分の状態を維持し、ループとアルゴリズムを記述するための強力なツールを提供しながら、
/* Using promises */
fetch('api-endpoint')
.then(resp => response.json())
.then(name => obj.name)
.then(x => console.log('Name: ', name))
//VS
/* As a generator function and assuming we have required node-fetch */
run(function *() {
const url = 'api-endpoint';
const resp = yield fetch(url);
const obj = yield response.json();
const name = yield obj.name;
console.log("Name available here: ", name);
}
function run(genFunc) {
const iterator = genFunc();
const iteration = iterator.next();
const promise = iteration.value();
promise.then(x => {
const additionalIterator = iterator.next(x);
const additionalPromise = iterator.value;
additionalPromise.then(y => iterator.next(y));
});
}
かなり曖昧です。あなたはそれらを使って同様のことをする方法を示すことができますか? – 4castle
問題ありません、例を追加します。 –