不使用 .then() 执行两个函数
Execute two functions without .then()
目前我有这个代码:
client.clearStore()
.then(() => someFunction())
.catch(e => logger.error('error', e));
它工作正常,但我在使用 .then()
进行测试时遇到问题,所以我想知道是否有另一种方法可以同时执行 client.clearStore()
和 someFunction()
但已经完成 client.clearStore()
那 returns 一个承诺。
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function
(async () => {
try {
const someFunction = await client.clearStore();
someFunction();
} catch (e) {
logger.error('error', e);
}
})();
您可以像这样使用 await
关键字
(async () => {
await client.clearStore();
someFunction()
});
try {
await client.clearStore();
someFunction()
} catch (e) {
logger.error('error', e)
}
但是 'then' 的解决方案对我来说看起来好多了并且异步执行(理论上,主线程在我们等待 'clearStore' 完成时正在执行)。我猜你在模拟 'clearStore' 到 return 承诺时遇到了问题?
目前我有这个代码:
client.clearStore()
.then(() => someFunction())
.catch(e => logger.error('error', e));
它工作正常,但我在使用 .then()
进行测试时遇到问题,所以我想知道是否有另一种方法可以同时执行 client.clearStore()
和 someFunction()
但已经完成 client.clearStore()
那 returns 一个承诺。
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function
(async () => {
try {
const someFunction = await client.clearStore();
someFunction();
} catch (e) {
logger.error('error', e);
}
})();
您可以像这样使用 await
关键字
(async () => {
await client.clearStore();
someFunction()
});
try {
await client.clearStore();
someFunction()
} catch (e) {
logger.error('error', e)
}
但是 'then' 的解决方案对我来说看起来好多了并且异步执行(理论上,主线程在我们等待 'clearStore' 完成时正在执行)。我猜你在模拟 'clearStore' 到 return 承诺时遇到了问题?