0% found this document useful (0 votes)
28 views

AP Lab 9

This document contains code for Lab 9 which demonstrates using callbacks, promises, and async/await in JavaScript to change the background color of a web page. It includes code for callback.js, promise.js, and asyawait.js files that are used to change the color when different buttons are clicked. It also contains code for two methods to repair an air conditioner using promises and async/await.

Uploaded by

Latest Techians
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views

AP Lab 9

This document contains code for Lab 9 which demonstrates using callbacks, promises, and async/await in JavaScript to change the background color of a web page. It includes code for callback.js, promise.js, and asyawait.js files that are used to change the color when different buttons are clicked. It also contains code for two methods to repair an air conditioner using promises and async/await.

Uploaded by

Latest Techians
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

Lab -9

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

<meta http-equiv="X-UA-Compatible" content="IE=edge">

<meta name="viewport" content="width=device-width, initial-scale=1.0">

<title>Lab 9 - 191b130</title>

<script src="callback.js"></script>

<script src="promise.js"></script>

<script src="asyawait.js"></script>

</head>

<body id="body">

<button onclick="startCallback()" class="btn1">Run using callback</button>

<button onclick="startPromise()" class="btn2">Run using promise</button>

<button onclick="startAsyncAwait()" class="btn3">Run using async await</button>

</body>

</html>

Callback.js-----

let colors = ["violet","indigo","blue","green","yellow"]

selectedColor = 0;

const startCallback = () => {

setTimeout(async () => {

let thisNum =(Math.random() * 1)

if (thisNum <= 0.9) {

console.log(thisNum)

document.getElementById('body').style.backgroundColor = "red";
if(selectedColor > 4) {

window.alert('ended');

return;

document.getElementById('body').style.backgroundColor = colors[selectedColor++]

startCallback();

else {

window.alert('Error number is not below 0.5');

}, 1500);

Promise.js------------

let colors1 = ["violet","indigo","blue","green","yellow"]

selectedColor1 = 0;

const timeFunction = Promise.resolve(33);

const startPromise = () => {

timeFunction.then((val)=>{

let thisNum =(Math.random() * 1)

if (thisNum <= 0.9) {

console.log(thisNum)

document.getElementById('body').style.backgroundColor = "red";

if(selectedColor > 4) {

window.alert('ended');

return;

document.getElementById('body').style.backgroundColor = colors1[selectedColor1++]

startCallback();
}

else {

window.alert('Error number is not below 0.5');

}})}
Method 2:-

let not_leaking = true;


let check = (time, work) => {
return new Promise((resolve, reject) => {
if (not_leaking) {
setTimeout(() => {
resolve(work())
}, time)
} else {
reject(console.log("AC is checked whether it is working fine or not))
}
});
};
check(2000, () => console.log("Not a leakage problem, starting repair"))
.then(() => {
return check(1000, () => console.log('Filters have been taken out'))
})
.then(() => {
return check(1000, () => console.log('Cleaned with water'))
})
.then(() => {
return check(1000, () => console.log('Fetch the hose'))
})
.then(() => {
return check(1000, () => console.log('Hose has been fetched and outddor
unit is cleaned'))
})
.then(() => {
return check(1000, () => console.log('Everything is dried up and filters
have been installed '))
})
.then(() => {
return check(1000, () => console.log('AC is checked whether it is working
fine or not'))
})
*/
Method 3:-

let not_leaking = true;


function time(ms) {
return new Promise((resolve, reject) => {
if (not_leaking) {
setTimeout(resolve, ms)
} else {
reject(console.log("AC is checked whether it is working fine or not"));
}
});
}
async function repair() {
try {
await time(2000);
console.log("Not a leakage problem, starting repair");
await time(2000);
console.log("Filters have been taken out");
await time(2000);
console.log("Cleaned with water");
await time(2000);
console.log("Fetch the hose");
await time(2000);
console.log("Hose has been fetched and outddor unit is cleaned");
await time(2000);
console.log("Everything is dried up and filters have been installed ");
} catch (error) {
console.log("AC fixed", error)
} finally {
console.log("AC is checked whether it is working fine or not")
}
}
repair();
Lab -9

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

<meta http-equiv="X-UA-Compatible" content="IE=edge">

<meta name="viewport" content="width=device-width, initial-scale=1.0">

<title>Lab 9 - 191b130</title>

<script src="callback.js"></script>

<script src="promise.js"></script>

<script src="asyawait.js"></script>

</head>

<body id="body">

<button onclick="startCallback()" class="btn1">Run using callback</button>

<button onclick="startPromise()" class="btn2">Run using promise</button>

<button onclick="startAsyncAwait()" class="btn3">Run using async await</button>

</body>

</html>

Callback.js-----

let colors = ["violet","indigo","blue","green","yellow"]

selectedColor = 0;

const startCallback = () => {

setTimeout(async () => {

let thisNum =(Math.random() * 1)

if (thisNum <= 0.9) {

console.log(thisNum)

document.getElementById('body').style.backgroundColor = "red";
if(selectedColor > 4) {

window.alert('ended');

return;

document.getElementById('body').style.backgroundColor = colors[selectedColor++]

startCallback();

else {

window.alert('Error number is not below 0.5');

}, 1500);

Promise.js------------

let colors1 = ["violet","indigo","blue","green","yellow"]

selectedColor1 = 0;

const timeFunction = Promise.resolve(33);

const startPromise = () => {

timeFunction.then((val)=>{

let thisNum =(Math.random() * 1)

if (thisNum <= 0.9) {

console.log(thisNum)

document.getElementById('body').style.backgroundColor = "red";

if(selectedColor > 4) {

window.alert('ended');

return;

document.getElementById('body').style.backgroundColor = colors1[selectedColor1++]

startCallback();
}

else {

window.alert('Error number is not below 0.5');

}})}
LAB5_1

----------------------------------------------------------------------------------
-- Company:
-- Engineer:
--
-- Create Date: 12:40:39 04/05/2022
-- Design Name:
-- Module Name: Lab5_1 - Behavioral
-- Project Name:
-- Target Devices:
-- Tool versions:
-- Description:
--
-- Dependencies:
--
-- Revision:
-- Revision 0.01 - File Created
-- Additional Comments:
--
----------------------------------------------------------------------------------
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;

-- Uncomment the following library declaration if using


-- arithmetic functions with Signed or Unsigned values
--use IEEE.NUMERIC_STD.ALL;

-- Uncomment the following library declaration if instantiating


-- any Xilinx primitives in this code.
--library UNISIM;
--use UNISIM.VComponents.all;

entity Lab5_1 is
generic (N: integer := 4);
Port ( r1 : in STD_LOGIC_VECTOR (N-1 downto 0);
r2 : in STD_LOGIC_VECTOR (N-1 downto 0);
r3 : in STD_LOGIC_VECTOR (N-1 downto 0);
r4 : in STD_LOGIC_VECTOR (N-1 downto 0);
s : in STD_LOGIC_VECTOR (1 downto 0);
o : out STD_LOGIC_VECTOR (N-1 downto 0));
end Lab5_1;

architecture Behavioral of Lab5_1 is

component fourtoone is
Port ( i0 : in STD_LOGIC;
i1 : in STD_LOGIC;
i2 : in STD_LOGIC;
i3 : in STD_LOGIC;
s0 : in STD_LOGIC;
s1 : in STD_LOGIC;
o : out STD_LOGIC);
end component;

begin

F: for i in 0 to N-1 generate


FA: fourtoone port map (r1(i), r2(i), r3(i), r4(i), s(0), s(1),o(i));
end generate;

end Behavioral;
TEST BENCH

BEGIN

-- Instantiate the Unit Under Test (UUT)


uut: Lab5_1 PORT MAP (
r1 => r1,
r2 => r2,
r3 => r3,
r4 => r4,
s => s,
o => o
);

-- Stimulus process
stim_proc: process
begin
-- hold reset state for 100 ns.

r1<="0000";
r2<="0011";
r3<="1100";
r4<="1111";
s<="00";
wait for 100 ns;

r1<="0000";
r2<="0011";
r3<="1100";
r4<="1111";
s<="01";
wait for 100 ns;

r1<="0000";
r2<="0011";
r3<="1100";
r4<="1111";
s<="10";
wait for 100 ns;

r1<="0000";
r2<="0011";
r3<="1100";
r4<="1111";
s<="11";
wait for 100 ns;

-- insert stimulus here

wait;
end process;

END;
Lab5_2

----------------------------------------------------------------------------------
-- Company:
-- Engineer:
--
-- Create Date: 09:49:18 05/02/2022
-- Design Name:
-- Module Name: Lab5_2 - Behavioral
-- Project Name:
-- Target Devices:
-- Tool versions:
-- Description:
--
-- Dependencies:
--
-- Revision:
-- Revision 0.01 - File Created
-- Additional Comments:
--
----------------------------------------------------------------------------------
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;

-- Uncomment the following library declaration if using


-- arithmetic functions with Signed or Unsigned values
--use IEEE.NUMERIC_STD.ALL;

-- Uncomment the following library declaration if instantiating


-- any Xilinx primitives in this code.
--library UNISIM;
--use UNISIM.VComponents.all;

entity Lab5_2 is
generic (N: integer := 4);
Port ( a : in STD_LOGIC_VECTOR (N-1 downto 0);
b : in STD_LOGIC_VECTOR (N-1 downto 0);
s : in STD_LOGIC_VECTOR (1 downto 0);
o : out STD_LOGIC_VECTOR (N-1 downto 0));
end Lab5_2;

architecture Behavioral of Lab5_2 is


signal x :STD_LOGIC_VECTOR (N-1 downto 0);
signal y :STD_LOGIC_VECTOR (N-1 downto 0);
signal z :STD_LOGIC_VECTOR (N-1 downto 0);
component fourtoone is
Port ( i0 : in STD_LOGIC;
i1 : in STD_LOGIC;
i2 : in STD_LOGIC;
i3 : in STD_LOGIC;
s0 : in STD_LOGIC;
s1 : in STD_LOGIC;
o : out STD_LOGIC);
end component;

begin

F: for i in 0 to N-1 generate


x(i) <= a(i) and b(i);
y(i) <= a(i) or b(i);
z(i) <= a(i) xor b(i);
FA: fourtoone port map (x(i), y(i), z(i), not a(i), s(0), s(1),o(i));
end generate;

end Behavioral;
TEST BENCH

BEGIN

-- Instantiate the Unit Under Test (UUT)


uut: Lab5_2 PORT MAP (
a => a,
b => b,
s => s,
o => o
);

-- Stimulus process
stim_proc: process
begin
a<="0000";
b<="1100";

s<="00";
wait for 100 ns;

s<="01";
wait for 100 ns;

s<="10";
wait for 100 ns;

s<="11";
wait for 100 ns;

wait;
end process;

END;
Lab 5_3

----------------------------------------------------------------------------------
-- Company:
-- Engineer:
--
-- Create Date: 10:15:21 05/02/2022
-- Design Name:
-- Module Name: Lab5_3 - Behavioral
-- Project Name:
-- Target Devices:
-- Tool versions:
-- Description:
--
-- Dependencies:
--
-- Revision:
-- Revision 0.01 - File Created
-- Additional Comments:
--
----------------------------------------------------------------------------------
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;

-- Uncomment the following library declaration if using


-- arithmetic functions with Signed or Unsigned values
--use IEEE.NUMERIC_STD.ALL;

-- Uncomment the following library declaration if instantiating


-- any Xilinx primitives in this code.
--library UNISIM;
--use UNISIM.VComponents.all;

entity Lab5_3 is
generic (N: integer := 4);
Port ( a : in STD_LOGIC_VECTOR (N-1 downto 0);
b : in STD_LOGIC_VECTOR (N-1 downto 0);
s : in STD_LOGIC_VECTOR (1 downto 0);
o : out STD_LOGIC_VECTOR (N-1 downto 0));
end Lab5_3;

architecture Behavioral of Lab5_3 is

begin
process(a,b,s)
begin
if(s="00") then
o<= (b(n-2 downto 0) & '0');
elsif(s="01") then
o<= (a(n-1) & a(n-1 downto 1));
elsif(s="10") then
o<= (b(0) & b(n-1 downto 1));
elsif(s="11") then
o<= (a(n-2 downto 0) & a(n-1));

end if;
end process;
end Behavioral;

Test Bench

begin
a<="0011";
b<="1100";

s<="00";
wait for 100 ns;

s<="01";
wait for 100 ns;

s<="10";
wait for 100 ns;

s<="11";
wait for 100 ns;
wait;
end process;

You might also like