1function resolveAfter2Seconds() {
2 return new Promise(resolve => {
3 setTimeout(() => {
4 resolve('resolved');
5 }, 2000);
6 });
7}
8
9async function asyncCall() {
10 console.log('calling');
11 const result = await resolveAfter2Seconds();
12 console.log(result);
13 // expected output: 'resolved'
14}
15
16asyncCall();
17
1console.log ('Starting');
2let image;
3
4fetch('coffee.jpg').then((response) => {
5 console.log('It worked :)')
6 return response.blob();
7}).then((myBlob) => {
8 let objectURL = URL.createObjectURL(myBlob);
9 image = document.createElement('img');
10 image.src = objectURL;
11 document.body.appendChild(image);
12}).catch((error) => {
13 console.log('There has been a problem with your fetch operation: ' + error.message);
14});
15
16console.log ('All done!');
1//Asynchronous JavaScript
2console.log('I am first');
3setTimeout(() => {
4 console.log('I am second')
5}, 2000)
6setTimeout(() => {
7 console.log('I am third')
8}, 1000)
9console.log('I am fourth')
10//Expected output:
11// I am first
12// I am fourth
13// I am third
14// I am second
1const printProcess = () => {
2 console.log('it will print 2nd');
3 var currentTime = new Date().getTime();
4 while (currentTime + 3000 >= new Date().getTime());
5 console.log('it will print after added 3 second with current time')
6}
7
8console.log('it will print 1st ');
9printProcess(); //follow arrow funtion after 1st print
10console.log('it will print at the end');
11//Expected output below:
12// it will print 1st
13// it will print 2nd
14// it will print after added 3 second with current time
15// it will print at the end