我在这样的函数上做call
:
const didRefresh = yield call(maybeRefresh);
console.log('call - maybeRefresh, didRefresh:', didRefresh); // i expect this to return before the fetch in refreshTask completes
maybeRefresh
就是这样:
function* maybeRefresh() {
const shouldRefresh = true;
if (shouldRefresh) {
yield fork(refreshTask);
console.log('did fork refresh task, should now return while refreshTask happens async');
}
const didRefresh = shouldRefresh;
return didRefresh;
}
function* refreshTask() {
console.log('will fetch');
const res = yield call(fetch, 'http://www.blah.com');
console.log('ok refresh done, status:', res.status); // however its not return until here
}
当我分叉refreshTask
时,我希望顶部的yield call
在获取之前返回一个值。然而即使"做叉子"得到记录,它似乎挂起,即使它准备返回。它会在返回时挂起,直到分叉任务refreshTask
完成,然后返回。这是yield call
的工作原理吗?它适用于孩子开始完成的任何异步内容吗?