gpt4 book ai didi

javascript - 生成器函数不显示我的数据,如何访问它?

转载 作者:行者123 更新时间:2023-11-30 11:29:17 24 4
gpt4 key购买 nike

我必须使用 sagas 和生成器函数调用 API。这是我的代码:

export function* fetchCreate(data) {
try {
const options = jsonBodyOptions(data);
const tagResponse = yield call(
fetchJson,
apiPath + '/fetch',
tagOptions
);
return tagResponse;
} catch (err) {
console.log(err);
}
}

export function* callFetch(data) {
const response = fetchCreate(data);
}

如果我打印 fetchCreate(),我会看到打印了生成器函数。

我想从同一文件中的另一个函数调用该生成器函数。我主要想要那个函数的响应,但基本上它返回一个生成器。如何从中检索响应?

最佳答案

尝试使用 yield call(...)

export function* callFetch(data) {
const response = yield call(fetchCreate, data);
}

如果 fetchJson 返回一个 promise,那么您可以选择将 fetchCreate 转换为一个返回 promise 而不是生成器的普通函数,因为 yield 调用 使用返回 promise 的函数。

export function fetchCreate(data) {
try {
const options = jsonBodyOptions(data);
return fetchJson(apiPath + '/fetch', tagOptions);
} catch (err) {
console.log(err);
}
}

关于javascript - 生成器函数不显示我的数据,如何访问它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46841345/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com