简体   繁体   English

Javascript“Promises”和函数式编程的“任务”之间有什么区别?

[英]What's the Difference Between Javascript “Promises” and functional Programming's “Task”?

Besides lazy execution, are Tasks and Promises pretty much the same thing? 除了懒惰的执行,任务和承诺几乎是一回事吗? When I refer to a task, I refer to a class that is in its most basic behavior like the following: 当我提到一个任务时,我指的是一个最基本的行为,如下所示:

class Task {
  constructor(then) {
    this.then = then;
  }
  map(mapper) {
    return new Task((resolve, reject) => this.then(
      x => resolve(mapper(x)),
      reject
    ))
  }
  flatMap(mapper) {
    return new Task((resolve, reject) => this.then(
      x => mapper(x).then(resolve, reject),
      reject
    ))
  }
}

What type of (class?) is a task/promise? 什么类型的(类?)是任务/承诺? I'm learning about functional programming approaches, but I don't think I've gotten to this type yet. 我正在学习函数式编程方法,但我认为我还没有达到这种类型。 Is it a type of monad? 它是一种monad吗?

Yes, the key thing is a monadic bind, or a flatMap in your case that has the signature: 是的,关键是monadic绑定,或者你的情况下有一个签名的flatMap

Task A -> A -> Task B -> Task B

With promises - that's the then method that: 有了承诺 - 这是then方法:

Promise A -> (A -> Promise B) -> Promise B
 this          onFulfilled        return value

In fact, both are instances of the Continuation Monad . 事实上,两者都是Continuation Monad的例子。 Lots of other things (like Rx streams) are instances of the continuation monad. 许多其他东西(如Rx流)是continuation monad的实例。

Promises in JavaScript however are specced with a slightly different (and uglier, for arguably practical reasons) signature with it being possible to also return a plain value from then , and there are exception semantics involved. 然而,JavaScript中的Promise具有稍微不同(并且更丑陋,因为可论证的实际原因)签名,从而可以then返回普通值,并且涉及异常语义。

There was a push for more "monadic" promises back in 2013 when they were specced, but it failed. 2013年,当他们被推测时,推动了更多的“monadic”承诺,但它失败了。 The current promises in JavaScript aren't really "monad"s per-se. JavaScript中的当前承诺并非真正的“monad”本身。

A. promises are one refactoring step further than callback.just that. A. promises是一个比callback更重构的重构步骤。就是这样。 if you have a function 如果你有一个功能

const f = x =>  x * x ;

1) Use Callback and remove return type 1)使用Callback并删除返回类型

you can pass a callback and remove the return type. 您可以传递回调并删除返回类型。 This the famous Continuation-passing style 这就是着名的延续传承风格

const f1 = (x,callback)=>callback(x*x);

What if we curry the action and return it as a result ?! 如果我们讨好行动并将其归还,该怎么办?! we could do this : 我们可以这样做:

const squareCont = (x)=>callback=>callback(x*x);

if you look at this for a long time and rename callback to resolve you will see that this is actually a promise. 如果你长时间看这个并重命名回调来解决,你会发现这实际上是一个承诺。

//i rewrited it to make it clearer
var squareCont = function(x){
   return function (resolve){return resolve (x*x);}
}

[Side Note : we could make it into a Promise if we enclose the callback into an object that exposes a then function like that : [旁注:如果我们将回调封装到一个暴露像那样的then函数的对象中,我们可以把它变成一个Promise:

const squarePromise = (x)=>({then:callback=>callback(x*x)});

squarePromise(2).then(r=>console.log(r))

check the fiddle here] B.Task is the Coninuation Co-Monad . 检查这里的小提琴] B.Task是Coninuation Co-Monad Because i cannot summarize it here you can read in more detail here : Promises & Continuation Monad in JavaScript and here Async/await aka Continuation Co- Monad in JavaScript 因为我不能在这里总结一下,你可以在这里详细阅读: JavaScript中的Promises和Continuation Monad以及这里的Async / await又名继续Co-Monad

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

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