Skip to content

Commit 8ae0d0f

Browse files
committed
More Addition
1 parent 069bfdb commit 8ae0d0f

File tree

1 file changed

+79
-46
lines changed

1 file changed

+79
-46
lines changed

Promise moreover.js

Lines changed: 79 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -285,66 +285,99 @@
285285

286286
//Another Example
287287

288-
function job(state) {
289-
return new Promise(function (resolve, reject) {
290-
if (state) {
291-
resolve("success");
292-
} else {
293-
reject("error");
294-
}
295-
});
296-
}
288+
// function job(state) {
289+
// return new Promise(function (resolve, reject) {
290+
// if (state) {
291+
// resolve("success");
292+
// } else {
293+
// reject("error");
294+
// }
295+
// });
296+
// }
297+
298+
// let promise = job(true);
299+
300+
// promise
301+
302+
// .then(function (data) {
303+
// console.log(data);
304+
305+
// return job(true);
306+
// })
307+
308+
// .then(function (data) {
309+
// if (data !== "victory") {
310+
// throw "Defeat";
311+
// }
312+
313+
// return job(true);
314+
// })
315+
316+
// .then(function (data) {
317+
// console.log(data);
318+
// })
297319

298-
let promise = job(true);
320+
// .catch(function (error) {
321+
// console.log(error);
299322

300-
promise
323+
// return job(false);
324+
// })
301325

302-
.then(function (data) {
303-
console.log(data);
326+
// .then(function (data) {
327+
// console.log(data);
304328

305-
return job(true);
306-
})
329+
// return job(true);
330+
// })
307331

308-
.then(function (data) {
309-
if (data !== "victory") {
310-
throw "Defeat";
311-
}
332+
// .catch(function (error) {
333+
// console.log(error);
312334

313-
return job(true);
314-
})
335+
// return "Error caught";
336+
// })
315337

316-
.then(function (data) {
317-
console.log(data);
318-
})
338+
// .then(function (data) {
339+
// console.log(data);
319340

320-
.catch(function (error) {
321-
console.log(error);
341+
// return new Error("test");
342+
// })
322343

323-
return job(false);
324-
})
344+
// .then(function (data) {
345+
// console.log("Success:", data.message);
346+
// })
325347

326-
.then(function (data) {
327-
console.log(data);
348+
// .catch(function (data) {
349+
// console.log("Error:", data.message);
350+
// });
328351

329-
return job(true);
330-
})
352+
//Promise.All , Incase of Multiple Asynchronous tasks have to do with single promise.
331353

332-
.catch(function (error) {
333-
console.log(error);
354+
// function job(delay) {
355+
// return new Promise(function(resolve) {
356+
// setTimeout(function() {
357+
// console.log('Resolving', delay);
358+
// resolve('done ' + delay);
359+
// }, delay);
360+
// });
361+
// }
334362

335-
return "Error caught";
336-
})
363+
// var promise = Promise.all([job(1000), job(2000), job(500), job(1500)]);
337364

338-
.then(function (data) {
339-
console.log(data);
365+
// promise.then(function(data) {
366+
// console.log('All done');
367+
// data.forEach(function(text) {
368+
// console.log(text);
369+
// });
370+
// });
371+
372+
//Promise.race
340373

341-
return new Error("test");
342-
})
374+
// function delay(time) {
375+
// return new Promise(function(resolve) {
376+
// setTimeout(resolve, time, 'success ' + time);
377+
// });
378+
// }
343379

344-
.then(function (data) {
345-
console.log("Success:", data.message);
346-
})
380+
// Promise.race([delay(500), delay(100)]).then(function(data) {
381+
// console.log(data);
382+
// });
347383

348-
.catch(function (data) {
349-
console.log("Error:", data.message);
350-
});

0 commit comments

Comments
 (0)