不会js能学nodejs吗_让我们来谈谈nodejs的承诺,我保证不会太久

不会js能学nodejs吗

Serverless architectures are becoming very popular nowadays and NodeJS runtime is an interesting option on AWS Lambda. However, an asynchronous runtime is not always a suitable solution. So, understanding promises in Javascript is essential to determine when to use NodeJS Lambda Functions on AWS.

如今,无服务器架构变得非常流行,NodeJS运行时是AWS Lambda上的一个有趣选项。 但是,异步运行时并不总是合适的解决方案。 因此,了解Java语言中的承诺对于确定何时在AWS上使用NodeJS Lambda函数至关重要。

In this article, we are going to wall through the characteristics of javascript in chronological order. As the title says, it won’t be long so bear with me!

在本文中,我们将按时间顺序介绍javascript的特性。 如标题所示,时间不长,请耐心等待!

:)

:)

首先,不可阻塞。 (First things first, non-blocking.)

It is a huge topic and can be considered a subject for another article. However is essential to keep in mind that NodeJS is a non-blocking language and uses a single Thread to run your operations.

这是一个巨大的主题,可以视为另一篇文章的主题。 但是,请记住,NodeJS是一种非阻塞语言,并且使用单个线程来运行您的操作,这一点至关重要。

What do you expect before running this snippet NodeJS code? Most people would expect something like this:

在运行此片段NodeJS代码之前,您期望什么? 大多数人会期望这样的事情:

1.Starting up
2.Two seconds!
3.Zero seconds!
4.Finishing up

But, remember that NodeJS is asynchronous by default so the result of this code is going to be this:

但是,请记住,NodeJS默认情况下是异步的,因此此代码的结果将是这样的:

1.Starting up
4.Finishing up
3.Zero seconds!
2.Two seconds!

Please notice that everything happens at the same time, go ahead and execute this code in your terminal, start to play around. You will start to understand what happens under the hood or optionally you can stay tuned a new article about Single Thread in details is coming soon. 👍

请注意,所有事情都同时发生,继续并在您的终端中执行此代码,然后开始玩。 您将开始了解幕后发生的事情,或者可以选择继续关注有关单线程的新文章,有关详细信息即将发布。 👍

回调就像承诺的祖先一样 (Callbacks are like promises’ ancestors)

So, we had Callbacks before that are the foundation of NodeJS, using it you have the feeling that things are happening in sequence.

因此,在此之前,我们已经有了Callbacks,这是NodeJS的基础,使用它,您会感觉事情是按顺序发生的。

A callback is a function called at the completion of a given task, this prevents any blocking, and allows other code to be run in the meantime.

回调是在完成给定任务时调用的函数,它可以防止任何阻塞,并允许同时运行其他代码。

To illustrate callbacks let’s use the following example creating “doWorkCallback” function. This function simply waits 2 seconds and return either an error message or an array, so you can run in your terminal and alternate the return to see both results displayed in your display.

为了说明回调,我们使用以下示例创建“ doWorkCallback ”函数。 该函数只需等待2秒钟,然后返回错误消息或数组,因此您可以在终端中运行并交替返回以查看显示在显示屏上的两个结果。

If you are not familiar with this ES6 syntax, using arrow functions. I also created this snippet with a simpler code with the same functions:

如果您不熟悉此ES6语法,请使用箭头功能。 我还使用具有相同功能的简单代码创建了此代码段:

Here we can clearly see that the “synchronous” feeling is generated by the callback that is the functions passed to our myFunction as an argument.

在这里我们可以清楚地看到,“同步”感觉是由回调函数产生的,该回调函数是作为参数传递给我们的myFunction的函数。

🤯

🤯

让我们去看看诺言的正式定义 (Let’s go to the official definition of promise)

The Promise object represents the eventual completion (or failure) of an asynchronous operation, and its resulting value.

Promise对象表示异步操作的最终完成(或失败)及其结果值。

When you have an asynchronous runtime by default, fetching operations will eventually require you to know the result of an operation to determine the next events in your application or script. It is one example where using promise is a suitable approach, so let’s move forward and investigate its origins.

默认情况下,当您具有异步运行时时,访存操作最终将需要您知道操作的结果,以确定应用程序或脚本中的下一个事件。 这是一个使用诺言是一种合适方法的示例,因此让我们继续研究它的起源。

This need for knowing the result of an asynchronous operation is not new and has appeared since the beginning. They are designed to manage asynchronous code, like callbacks enhancement.

从一开始就已经出现了这种了解异步操作结果的需求。 它们旨在管理异步代码,例如回调增强。

Here is an example:

这是一个例子:

Using the same example before, but now using promises we can see their 3 states that are pending, fulfilled and rejected. So, you can monitor and take actions in your application or script based on those states.

之前使用相同的示例,但是现在使用promise,我们可以看到它们的三个状态分别处于待处理,已实现和已拒绝。 因此,您可以根据这些状态监视应用程序或脚本中的行为并采取相应措施。

Don’t be shy, go ahead and run this code on your computer! :)

别害羞,继续在您的计算机上运行此代码! :)

I said it would not be long, so I am stopping here but we could extend this subject to callback hell, promise chaining and async-await topics. But the aim of this text is to explain promise in its pure form. It is essential to know the problem that these technologies solves and consider these aspects when choosing your AWS Lambda runtime.

我说不会太久,所以我在这里停止,但是我们可以将此主题扩展到回调地狱,承诺链接和异步等待主题。 但是本文的目的是要以纯形式解释诺言。 在选择AWS Lambda运行时时,必须了解这些技术可以解决的问题并考虑这些方面。

Stay tuned for more content!

请继续关注更多内容!

At DNX Solutions, we work to bring a better cloud and application experience for digital-native startups in Australia.

DNX Solutions ,我们致力于为澳大利亚的数字原生创业公司带来更好的云和应用程序体验。

Our current focus areas are AWS, Well-Architected Solutions, Containers, ECS, Kubernetes, Continuous Integration/Continuous Delivery and Service Mesh and Data Solutions (movement, transformation, lakes, warehouses and analytics).

我们当前的重点领域是AWS,结构完善的解决方案,容器,ECS,Kubernetes,持续集成/持续交付以及服务网格和数据解决方案(移动,转换,湖泊,仓库和分析)。

We are constantly hiring cloud engineers for our Sydney office, focusing on cloud-native concepts.

我们一直在为悉尼办事处招聘云工程师,专注于云原生概念。

Check our open-source projects at https://github.com/DNXLabs and follow us on our Linkedin.

https://github.com/DNXLabs上 检查我们的开源项目, 并在 Linkedin 上关注我们

翻译自: https://medium.com/dnx-labs/lets-talk-about-nodejs-promises-i-promise-it-wont-be-long-dcd145b1b544

不会js能学nodejs吗

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值