Skip to content

Commit d17ac45

Browse files
committed
Q Promise
1 parent 35d02d3 commit d17ac45

File tree

1 file changed

+55
-3
lines changed

1 file changed

+55
-3
lines changed

README.md

Lines changed: 55 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3127,7 +3127,7 @@ module.exports = {
31273127

31283128
The `jwt.sign()` method takes a payload and the secret key defined in `config.js` as parameters. It creates a unique string of characters representing the payload. In our case, the payload is an object containing only the id of the user.
31293129

3130-
**[[Read More](https://github.com/auth0/node-jsonwebtoken)]**
3130+
* **[[Reference](https://www.npmjs.com/package/jsonwebtoken)]**
31313131

31323132
<div align="right">
31333133
<b><a href="#">↥ back to top</a></b>
@@ -3140,7 +3140,7 @@ The `jwt.sign()` method takes a payload and the secret key defined in `config.js
31403140
Microservices are a style of **service-oriented architecture** (SOA) where the app is structured on an assembly of interconnected services. With microservices, the application architecture is built with lightweight protocols. The services are finely seeded in the architecture. Microservices disintegrate the app into smaller services and enable improved modularity.
31413141

31423142
<p align="center">
3143-
<img src="assets/monolithic-and-microservices-architecture.jpg" alt="Microservices" width="600px" />
3143+
<img src="assets/monolithic-and-microservices-architecture.jpg" alt="Microservices" width="400px" />
31443144
</p>
31453145

31463146
There are few things worth emphasizing about the superiority of microservices, and distributed systems generally, over monolithic architecture:
@@ -3220,7 +3220,59 @@ module.exports = controllers;
32203220
<b><a href="#">↥ back to top</a></b>
32213221
</div>
32223222

3223-
#### 74Q. ***How to use Q promise in Node.js?***
3223+
## 74Q. ***How to use Q promise in Node.js?***
3224+
3225+
A promise is an object that represents the return value or the thrown exception that the function may eventually provide. A promise can also be used as a proxy for a remote object to overcome latency.
3226+
3227+
Promise is relatively an easy implementation for asynchronous operation. The promise object returned from the function represents an operation which is not completed yet, but it guarantees to the caller of the operation that the operation will be completed in future.
3228+
3229+
Promise has the following states:
3230+
3231+
* **Pending** - asynchronous operation is not yet completed.
3232+
* **Fulfilled** - asynchronous operation is completed successfully.
3233+
* **Rejected** - asynchronous operation is terminated with an error.
3234+
* **Settled** - asynchronous operation is either fulfilled or rejected.
3235+
* **Callback** - function is executed if the promise is executed with value.
3236+
* **Errback** - function is executed if the promise is rejected.
3237+
3238+
**Moving to Promises from Callback**
3239+
3240+
On the first pass, promises can mitigate the **Pyramid of Doom**: the situation where code marches to the right faster than it marches forward.
3241+
3242+
```js
3243+
step1(function (value1) {
3244+
step2(value1, function(value2) {
3245+
step3(value2, function(value3) {
3246+
step4(value3, function(value4) {
3247+
// Do something with value4
3248+
});
3249+
});
3250+
});
3251+
});
3252+
```
3253+
3254+
With a promise library, it can flatten the pyramid.
3255+
3256+
```js
3257+
Q.fcall(promisedStep1)
3258+
.then(promisedStep2)
3259+
.then(promisedStep3)
3260+
.then(promisedStep4)
3261+
.then(function (value4) {
3262+
// Do something with value4
3263+
})
3264+
.catch(function (error) {
3265+
// Handle any error from all above steps
3266+
})
3267+
.done();
3268+
```
3269+
3270+
* **[[Reference](https://www.npmjs.com/package/q)]**
3271+
3272+
<div align="right">
3273+
<b><a href="#">↥ back to top</a></b>
3274+
</div>
3275+
32243276
#### 75Q. ***How to use locale (i18n) in Node.js?***
32253277
#### 76Q. ***How to implement Memcached in Node.js?***
32263278
#### 77Q. ***Explain error handling in Node.js?***

0 commit comments

Comments
 (0)