未加星标

node: Use ES6 style Promises with Node.js

字体大小 | |
[前端(javascript) 所属分类 前端(javascript) | 发布者 店小二03 | 时间 2017 | 作者 红领巾 ] 0人收藏点击收藏
Use ES6 style Promises with Node.js

5:56 Node.js lesson byWill Button

Using promises can be confusing. In this lesson, I show you how to create promises to chain functions together in a specified order. You'll also learn how to pass the return value of promises as the input parameters of promises further down the chain. All examples in this lesson utilize native ES6 style promises, which are fully supported by recent versions of node.js without any dependencies.

Share this lesson: Facebook Twitter Google+

Get the Code Now

click to level up

Comment Guidelines

Member comments are a way for PRO Members to communicate, interact, and ask questions about a lesson.

Commenting Guidelines

Here are some basic guidelines for commenting on egghead.io.

Be on-topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contactclick here.

Avoid meta-discussion

Is your comment resembles:

This was great! This was horrible! I didn't like this because it didn't match my skill level. +1

It will likely be deleted as "meta".

Code problem?

Should be accompanied by code! JSFiddle, Plunker, CodePen, etc all provide a way to share code and discuss it in context.

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

egghead.io comment guidelines

egghead.io 6 minutes ago

Using promises can be confusing. In this lesson, I show you how to create promises to chain functions together in a specified order. You'll also learn how to pass the return value of promises as the input parameters of promises further down the chain. All examples in this lesson utilize native ES6 style promises, which are fully supported by recent versions of node.js without any dependencies.

I have these four functions here named One, Two, Three, and Four, and I want to execute them in order. I can do that just by calling each one. When I run it, they run in the order that I specified, because each one is returning immediately. What if they didn't return immediately?

What if they were doing something like an API call, where you didn't know how long it was going to take the server being called to return? We can simulate that with a setTimeout, and I can vary the amount of time that each one is taking to return here. Now if we run that, you can see that they didn't return in the order that they were called. I still called them in the order of One, Two, Three, Four, but they returned in the order of Three, Two, One, Four.

This is where promises come into play. We can return a promise, and then that promise will get fulfilled whenever the function has completed. We can use that to control the flow of our application. Let's re-factor it and take a look at how that's going to look.

I'm going to re-factor my function, so that the first thing it does is it returns a new promise. That promise is going to have a parameter called Resolve. Then we're going to have our setTimeout. We can logout the name of the function, again specifying our timeout. After we log this out, we want to call the Resolve parameter, which is actually a function of the promise.

I'm going to do the same thing for the rest of these functions, just modifying the time that it waits and the output of the console.log statement. The premise is still the same. It's just creating a promise, and then, after the timeout, we're resolving that promise.

We need to change the way that we're going to call this to execute, as well. We're still going to call our first function, but because we're using promises now, we now have a Then method. What that's going to do is we call the function, which returns a promise, then when that promise is resolved, we can call function Two, then Three, and then Four.

Let's take a look at running that. Now they're back running in order, One, Two, Three, Four. Let's take this a little step further. I want to give each one of these functions a parameter. We'll call it Ops. In the console.log statement, I'm going to logout that parameter, rather than a hard-coded number. I'm also going to return that whenever I call the Resolve method.

In function Two, I'm going to increment that counter ops, and then again we'll log it out and return it as part of our Resolve function. Same thing for function number three, and ditto for function number four. Now if I supply a parameter to the function named One, and then run that, we get the original output that we're shooting for of One, Two, Three, Four.

Let's take a look and see how that worked. Function One receives a parameter called Ops, and that parameter, when the timeout expired, we logged it out. Then when we called the Resolve method, we returned the Ops parameter. What that does is any value returned by the resolve method gets passed as a parameter to the next function in the chain.

In this case, we called function Two, supplying the results, the return parameter of function One as our parameter. We incremented that counter, which resulted in the number two being output. Then we returned that in our Resolve method and called function Three, which again incremented it, output it, returned as the parameter, and again to function Four.

That's pretty cool when things work right, but what about when things don't work right, when we shouldn't be resolving our promise? Promises actually have a second parameter called Reject, as well. We can check for condition. If that condition is true, we decide we want to reject that promise, we can do so by calling the Reject method.

Again. it has a return value that we can supply that's either an object, or a string of text in this case. Then we'll have our Else, and we'll execute our setTimeout. Then to catch this at the end of our statement here, we're going to use the .catch, which has a parameter we'll call Error, and then we'll just logout our error.

When we run that now, we've called the function One, passed in the parameter of one. The output of function One was the parameter passed in, which was one. That gets passed as a parameter to function Two, where we determine that the Ops variable was equal to one, so we rejected that promise with the message found in error, which then triggered our catch block, output the error, and stopped the promise or stopped the sequence of events.

One last thing to point out on this is that you'll notice I didn't require any NPM modules or any external dependencies. These ES6 style promises are now natively supported by any of the recent versions of node.js, so you can just use them right out of the box.

本文前端(javascript)相关术语:javascript是什么意思 javascript下载 javascript权威指南 javascript基础教程 javascript 正则表达式 javascript设计模式 javascript高级程序设计 精通javascript javascript教程

主题: Node.jsFacebookTwitter
分页:12
转载请注明
本文标题:node: Use ES6 style Promises with Node.js
本站链接:http://www.codesec.net/view/531454.html
分享请点击:


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 前端(javascript) | 评论(0) | 阅读(39)