return fetch(url, {
credentials: 'same-origin',
...options
})
.then(response => response.json())
.then(function*(response) {
console.log("httpStatusCode", response.httpStatusCode)
})
以上可能吗? 当回调函数是生成器时,我没有得到控制台输出,这意味着控件没有传递给回调函数(生成器)。 我想这样做的真正原因是我必须使用上面回调中 redux-saga 的“call”辅助函数来调用另一个获取请求,该函数只能从生成器函数中调用。
最佳答案
我想你可以做到。当生成器函数运行时,它将生成一个生成器对象,并将其传递到可以启动生成器的下一个 then 阶段。让我们看看...
var pr = Promise.resolve(42);
pr.then(function*(n){ yield n; yield n / 2; yield 37})
.then(it => {console.log(it.next().value);
console.log(it.next().value);
console.log(it.next().value);
});
关于javascript - 如何在 fetch 调用返回的 Promise 中使用生成器函数作为回调?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38760406/