What is Promises

const fetchUser = () => {
return new Promise((resolve, reject) => {
setText('Fetching User...');
setTimeout(() => {
resolve({ name: "Navnit" });
}, 2000);
});
};

fetchUser().then((response)=> console.log(response)); // Promise Resolved with { name: "Navnit" }
// We can write in other way by passing console.log function directly to then
fetchUser().then((response)=> console.log(response)) // Usefull while debugging
const url = "https://jsonplaceholder.typicode.com/posts/1";

const waitTillEnd = async url => {
try {
const response = await fetch(url);
const json = await response.json();
console.log(json);
} catch (error) {
console.log(error);
}
};

waitTillEnd(url);

Promise.all()

const promise1 = Promise.resolve(3);
const promise2 = 42;
const promise3 = new Promise(function(resolve, reject) {
setTimeout(resolve, 100, 'foo');
});

Promise.all([promise1, promise2, promise3]).then(function(values) {
console.log(values);
}); // expected output: Array [3, 42, "foo"]

We craft digital products for your business growth. Development | Marketing | Branding | Strategic Partnership