Skip to content

Instantly share code, notes, and snippets.

@detoner777
Created September 22, 2019 06:57
Show Gist options
  • Save detoner777/448fc53e5e6229e4cae40aa2ccae7ec7 to your computer and use it in GitHub Desktop.
Save detoner777/448fc53e5e6229e4cae40aa2ccae7ec7 to your computer and use it in GitHub Desktop.
console.log("Request data...");
// setTimeout(() => {
// console.log("Preparing data...");
// const backendData = {
// server: "aws",
// port: 2000,
// status: "working"
// };
// setTimeout(() => {
// backendData.modifed = true;
// console.log("Data received", backendData);
// }, 2000);
// }, 2000);
// const p = new Promise((resolve, reject) => {
// setTimeout(() => {
// console.log("Preparing data...");
// const backendData = {
// server: "aws",
// port: 2000,
// status: "working"
// };
// resolve(backendData);
// }, 2000);
// });
// p.then(data => {
// return new Promise((resolve, reject) => {
// setTimeout(() => {
// data.modified = true;
// resolve(data);
// }, 2000);
// });
// })
// .then(clientData => {
// clientData.fromPromise = true;
// return clientData;
// })
// .then(data => {
// console.log("Modified", data);
// })
// .catch(err => console.error("Error", err))
// .finally(() => console.log("finally"));
const sleep = ms => {
return new Promise(resolve => {
setTimeout(() => resolve(), ms);
});
};
// sleep(2000).then(() => console.log("After 2 sec"));
// sleep(3000).then(() => console.log("After 3 sec"));
Promise.all([sleep(2000), sleep(5000)]).then(() => {
console.log("All promises");
});
Promise.race([sleep(2000), sleep(5000)]).then(() => {
console.log("Race promises");
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment