Skip to content

Commit 7941b0d

Browse files
committed
Update index.md
1 parent cc934f7 commit 7941b0d

File tree

1 file changed

+13
-52
lines changed

1 file changed

+13
-52
lines changed

index.md

Lines changed: 13 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1250,24 +1250,21 @@ Once a promise is fulfilled or rejected, it is immutable (i.e. it can never chan
12501250

12511251
```js
12521252
/**
1253-
* GET Request using Axios
1253+
* Promise
12541254
*/
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+
});
12581263

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+
}
12671266

1268-
app.listen(3000, function () {
1269-
console.log(`App listening at http://localhost:3000/`);
1270-
});
1267+
console.log(getSum(10, 20)); // Promise { 30 }
12711268
```
12721269

12731270
<div align="right">
@@ -1309,43 +1306,7 @@ Content-Security-Policy: default-src 'self' *.http://sometrustedwebsite.com
13091306
## Q. How to make an HTTP POST request using Node.js?
13101307

13111308
```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
13491310
```
13501311

13511312
<div align="right">

0 commit comments

Comments
 (0)