gpt4 book ai didi

javascript - Redux 传奇 : Await Promise

转载 作者:行者123 更新时间:2023-12-02 00:10:58 24 4
gpt4 key购买 nike

我有一个已经调用并且必须等待的 promise 。基本上:

const foo = () => Promise.resolve('foo'); // The real promise takes time to resolve.

const result = foo();

await result; // This line has to happen in the saga

我怎样才能等待未决的 promise ?如果我将它包装在 call 中,Redux Saga 会尝试调用它并崩溃。

最佳答案

如果您处于传奇中,只需yield promise 即可。 Redux saga 将等待它解决,然后恢复 saga,就像 awaitasync 函数中所做的那样:

const foo = () => Promise.resolve('foo');
const resultingPromise = foo();

function* exampleSaga() {
const result = yield resultingPromise;
console.log(result); // 'foo'
}

如果 promise 可能会被拒绝,您可以将其包装在 try catch 中:

try {
const result = yield resultingPromise;
console.log(result);
} catch(err) {
console.log('promise rejected', err);
}

关于javascript - Redux 传奇 : Await Promise,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59141316/

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