添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I'm having troubles making Saga work with an API that works with Promises.

const result = yield call(
    () => {
        return this.getDays().then((_result) => {
            console.log('result inside');
            console.log(_result);
console.log('result outside');
console.log(result);

When I read the value the value of _result inside the then(), it prints the correct value but I cannot seem to made it work outside of the yield call to further pass it down to the action dispatch (yield put).

Following the lead of similar questions I tried a bunch of stuff:

I've tried putting a return of _result inside the then(), as well as a Promise.resolve(_result).

I've also tried returning the entire yield call and putting a variable outside to assign it in the then() but nothing seems to work.

It's the first time I'm working with Promises and generators functions and Saga and Redux and React in general so I must definitely be missing something.

Also the console.log() inside then() is printed after the console.log() at the end.

Any ideas? Thank you.

const result = yield call(
    () => new Promise((resolve) => {
        this.getDays().then((_result) => {
            resolve(_result);
                It is probably worth pointing out that in this scenario, it looks like getDays() is async... if that's the case, this can be simplified to just be const result = yield call(this.getDays);  I didn't want to add an answer for this because your answer is 100% correct - just shortened the suggestion somewhat :)
– CodyKnapp
                Oct 1, 2020 at 15:08
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.