J. *_*ers 1 javascript redux-saga
我有一个已经被调用并且必须等待的承诺。基本上:
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
Run Code Online (Sandbox Code Playgroud)
我如何等待未决的承诺?如果我将它包装在call
Redux Saga 中,则尝试调用它并崩溃。
如果你在一个传奇,简单yield
的承诺。Redux saga 将等待它解决,然后恢复 saga,就像await
在async
函数中所做的一样:
const foo = () => Promise.resolve('foo');
const resultingPromise = foo();
function* exampleSaga() {
const result = yield resultingPromise;
console.log(result); // 'foo'
}
Run Code Online (Sandbox Code Playgroud)
如果承诺可能会拒绝,您可以将其包装在 try catch 中:
try {
const result = yield resultingPromise;
console.log(result);
} catch(err) {
console.log('promise rejected', err);
}
Run Code Online (Sandbox Code Playgroud)