2017-10-20 85 views
1

我必须使用sagas和generator函数调用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(),我看到发生器功能打印。

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

回答

3

使用​​

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

如果fetchJson返回一个承诺,那么你可以选择转换为fetchCreate返回一个承诺,而不是一台发电机,因为yield call作品与承诺回报功能的普通函数尝试。

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