Node.js承诺,异步或仅回调 [英] Node.js promises, async, or just callbacks

查看:66
本文介绍了Node.js承诺,异步或仅回调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您可以向初学者解释承诺,异步和回调之间的区别。这些术语如何相互关联?这些是同一回事吗?不同的东西?

解决方案

异步是启动计算,提供函数或注册处理程序的常规设计模式最终将在计算完成时调用计算结果(这与在开始其他工作之前阻塞并等待计算完成相反)。如果没有异步,则同时启动多个计算需要使用线程。



回调是指您提供给异步计算的函数,该函数将在该计算时被调用完成。之所以称为回调,是因为它被异步函数调用,并且在被调用时,它将控制流返回到您可以控制的代码中。



Promise是特定的JavaScript原型和关联的框架,可为以异步样式编写的代码带来一致性。 Promise表示可能已完成或未完成(成功或未成功)的异步计算,并且提供了一种对结果进行操作或处理错误的方法,而与异步计算的完成状态无关。 Promise API还提供实用程序,用于组合多个异步计算的输出(例如,等待一组异步计算中的一个或全部完成下一次计算)。



给出一个没有承诺的简单示例:

  var addThen = function(a,b,handler){
var结果= a + b;
handler(result);
};

// ...
addThen(2,3,console.log); //打印5
// ...

以及与Promise等效的内容:

  var add = function(a,b){
return Promise.resolve(a + b);
};

// ...
add(2,3).then(console.log); //打印5
// ...

可以使用和编写异步代码没有Promises,但是使用Promise的主要优点是一致性(例如,成功回调和失败回调在参数列表中的位置,是否支持失败回调等),并且可以将它们组合在一起。 p>

Can you explain to a beginner the differences between "promises", "async", and "callbacks". How do these terms relate to one another? Are these the same thing? Different things? When do I use which?

解决方案

Async is the general design pattern of starting a computation and providing a function or registering a handler that will eventually get called with the result of the computation when it has completed (as opposed to blocking and waiting for the computation to complete before starting additional work). Without async, starting multiple computations simultaneously requires the use of threads.

A "callback" refers to the function that you provide to an async computation that will get invoked when that computation completes. It is called a "callback" because it gets called by the async function, and in getting called, it returns the flow of control back into code that you have control over.

A "Promise" is a specific JavaScript prototype and associated framework that brings consistency to code that is written in an asynchronous style. A Promise represents an async computation that may or may not have completed (successfully or unsuccessfully) and provides a means of operating on the result or handling errors regardless of the state of completion of the asynchronous computation. The Promise API also provides utilities for combining the outputs of multiple asynchronous computations (such as waiting for one of or all of a set of asynchronous computations to complete before the next calculation).

To give a simple example without Promises:

var addThen = function(a, b, handler) {
  var result = a + b;
  handler(result);
};

// ...
addThen(2, 3, console.log);  // prints 5
// ...

And the equivalent with Promise:

var add = function(a, b) {
  return Promise.resolve(a + b);
};

// ...
add(2, 3).then(console.log); // prints 5
// ...

Async code can be written both with and without Promises, but the primary advantage of using Promise is consistency (e.g. where in the argument list the success callback and failure callbacks go, whether a failure callback is supported or not, etc.) and support libraries that can combine them together.

这篇关于Node.js承诺,异步或仅回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆