等待在函数式编程JavaScript中没有位置

等待在函数式编程JavaScript中没有位置

本文介绍了异步/等待在函数式编程JavaScript中没有位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用JavaScript学习monad. Promise本身就是monad,并且是功能上处理异步副作用的正确方法. es7新播放器异步/等待如何,我们可以在JavaScript的函数编程中使用它吗?我觉得它的定义很明确,保证单子可以返回命令式方法.还是有一种足够明智地使用异步/等待的方法来使函数编程更加容易?

I am learning monad with JavaScript. Promise itself is monad, and is the right way to handle async side effects functionally. How about the es7 new player async/await, can we use it in functional programming of JavaScript? I feel like it falls well defined promise monad back to imperative way. Or there is a way to use async/await wisely enough to make function programming even easier?

推荐答案

您仍然可以使用它以功能样式编写异步代码. async/await只是糖,就像表示法在Haskell中一样(尽管它仅限于Promise而非通用Monad).如果您在编写带有承诺的代码时觉得async/await可以简化它,请使用它.

You still can write asynchronous code in a functional style with it. async/await is nothing but sugar, just like do notation is in Haskell (though it is restricted to promises and not generic monads). If you are writing code with promises and feel that async/await would simplify it, then use it.

请注意,promise通常使用副作用(而在时域中,异步本身可以算作一种效果),因此命令式风格通常不是错误的方法.毕竟,即使是功能程序也会产生副作用.

Notice that promises are often using side-effects (and asynchrony itself, by being in the time domain, might count as an effect), so an imperative style is often not the wrong approach. After all, even functional programs carry out side effects.

这篇关于异步/等待在函数式编程JavaScript中没有位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-16 07:08