@@ -1250,24 +1250,21 @@ Once a promise is fulfilled or rejected, it is immutable (i.e. it can never chan
1250
1250
1251
1251
``` js
1252
1252
/**
1253
- * GET Request using Axios
1253
+ * Promise
1254
1254
*/
1255
- const express = require (" express" );
1256
- const app = express ();
1257
- const axios = require (" axios" );
1255
+ function getSum (num1 , num2 ) {
1256
+ const myPromise = new Promise ((resolve , reject ) => {
1257
+ if (! isNaN (num1) && ! isNaN (num2)) {
1258
+ resolve (num1 + num2);
1259
+ } else {
1260
+ reject (new Error (" Not a valid number" ));
1261
+ }
1262
+ });
1258
1263
1259
- app .get (" /" , async (req , res ) => {
1260
- try {
1261
- const response = await axios .get (" https://jsonplaceholder.typicode.com/todos/1" );
1262
- res .status (200 ).json (response .data );
1263
- } catch (err) {
1264
- res .status (500 ).json ({ message: err });
1265
- }
1266
- });
1264
+ return myPromise;
1265
+ }
1267
1266
1268
- app .listen (3000 , function () {
1269
- console .log (` App listening at http://localhost:3000/` );
1270
- });
1267
+ console .log (getSum (10 , 20 )); // Promise { 30 }
1271
1268
```
1272
1269
1273
1270
<div align =" right " >
@@ -1309,43 +1306,7 @@ Content-Security-Policy: default-src 'self' *.http://sometrustedwebsite.com
1309
1306
## Q. How to make an HTTP POST request using Node.js?
1310
1307
1311
1308
``` js
1312
- const https = require (' https' )
1313
-
1314
-
1315
- const obj = {
1316
- " userId" : 1 ,
1317
- " id" : 1 ,
1318
- " title" : " whatever" ,
1319
- " completed" : false
1320
- }
1321
-
1322
- const data = JSON .stringify (obj)
1323
-
1324
- const options = {
1325
- hostname: ' jsonplaceholder.typicode.com' ,
1326
- port: 443 ,
1327
- path: ' /todos' ,
1328
- method: ' POST' ,
1329
- headers: {
1330
- ' Content-Type' : ' application/json' ,
1331
- ' Content-Length' : data .length
1332
- }
1333
- }
1334
-
1335
- const req = https .request (options, res => {
1336
- console .log (` statusCode: ${ res .statusCode } ` )
1337
-
1338
- res .on (' data' , d => {
1339
- process .stdout .write (d)
1340
- })
1341
- })
1342
-
1343
- req .on (' error' , error => {
1344
- console .error (error)
1345
- })
1346
-
1347
- req .write (data)
1348
- req .end ()
1309
+ // ToDo
1349
1310
```
1350
1311
1351
1312
<div align =" right " >
0 commit comments