简体   繁体   English

Javascript异步Curry函数ES6

[英]Javascript Async Curry Function ES6

Is there a way to make a curry function async? 有没有办法使咖喱功能异步?

Suppose I have the event handler function below (in my actual use case it is not an event handler), how could I update it to run async? 假设我下面有事件处理函数(在我的实际用例中,它不是事件处理函数),如何更新它以运行异步? My end goal is to have a curried function that I can await (it will be returning a promise). 我的最终目标是拥有一个我可以等待的咖喱函数(它将返回一个承诺)。

foo = () => (event) => {
   this.setState({something:event.target.value});
}

Keep in mind the 'await' keyword can only be used inside an async function. 请记住,“ await”关键字只能在异步函数内使用。 So, even if you defined an async function - and you called that function from somewhere else - you won't be able to utilize the 'await' keyword. 因此,即使您定义了一个异步函数-并且从其他地方调用了该函数-您也将无法使用'await'关键字。 I believe you're looking for this: 我相信您正在寻找这个:

const foo = () => async (event) => {
    await performAnAsyncEvent(event);
    this.setState({something:event.target.value});
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM