forked from Safnaj/JavaScript-ES6
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathpromise.js
More file actions
39 lines (30 loc) · 996 Bytes
/
promise.js
File metadata and controls
39 lines (30 loc) · 996 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
//Ex - 1
const http = require('http');
function fetchWebPage(url){
return new Promise((resolve, reject) => {
http.get((url, response => {
let responseData;
response.on('data', data => responseData = responseData+data);
response.on('end', () => resolve(responseData));
response.on('error', reject);
}));
});
}
fetchWebPage('https://www.ahamedsafnaj.com')
.then(data => console.log(data.length))
.catch(error => console.log(error))
.finally(() => console.log("finished"))
//Ex - 2
renderWebPage();
function renderWebPage(){
let response = '';
response = fetchWebPage('https://www.ahamedsafnaj.com');
console.log("Ex 2 " + response.length); //Undefined -> Bcz its not wait till the response completed
}
//EX - 3
renderWebPageAsync();
async function renderWebPageAsync(){
let response = '';
response = await fetchWebPage('https://www.ahamedsafnaj.com');
console.log("Ex 3 " + response.length);
}