Skip to content

Commit e6cd7fa

Browse files
authored
Merge pull request #5 from Ansari1120/main
Main
2 parents 1f39d27 + 8ae0d0f commit e6cd7fa

File tree

1 file changed

+309
-12
lines changed

1 file changed

+309
-12
lines changed

Promise moreover.js

Lines changed: 309 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -72,15 +72,312 @@
7272

7373
// Calling resolve multiple times
7474

75-
var promise = new Promise(function(resolve, reject) {
76-
setTimeout(function() {
77-
resolve('hello world 1');
78-
resolve('hello world 2');
79-
resolve('hello world 3');
80-
resolve('hello world 4');
81-
}, 1000);
82-
});
83-
84-
promise.then(function success(data) {
85-
console.log(data);
86-
});
75+
// var promise = new Promise(function(resolve, reject) {
76+
// setTimeout(function() {
77+
// resolve('hello world 1');
78+
// resolve('hello world 2');
79+
// resolve('hello world 3');
80+
// resolve('hello world 4');
81+
// }, 1000);
82+
// });
83+
84+
// promise.then(function success(data) {
85+
// console.log(data);
86+
// });
87+
88+
//promise chain
89+
90+
// function test() {
91+
// return job().then(function(data) {
92+
// doSomething(data);
93+
// });
94+
// }
95+
96+
//The pyramid of doom
97+
98+
// function test() {
99+
// return job()
100+
101+
// .then(function() {
102+
// return job2();
103+
// })
104+
105+
// .then(function() {
106+
// return job3();
107+
// })
108+
109+
// .then(function() {
110+
// return job4();
111+
// })
112+
113+
// .then(function() {
114+
// doSomething();
115+
// });
116+
// }
117+
118+
//The ghost promise
119+
120+
// function job() {
121+
// if (test) {
122+
// return aNewPromise();
123+
// } else {
124+
// return Promise.resolve(42); // return an anto-resolved promise with `42` in data.
125+
// }
126+
// }
127+
128+
//If you want to create an auto-rejected promise, use Promise.reject.
129+
130+
//The forgotten promise
131+
132+
// function test() {
133+
// var promise = job();
134+
135+
// return new Promise(function(resolve, reject) {
136+
// promise.then(function(data) {
137+
// data = doSomething(data);
138+
// resolve(data);
139+
// }, function(error) {
140+
// reject(error);
141+
// });
142+
// });
143+
// }
144+
//You call a method that returns a promise but you create your own promise:
145+
146+
//Or
147+
148+
// function test() {
149+
// return job().then(function(data) {
150+
// return doSomething(data);
151+
// });
152+
// }
153+
154+
//The catch method on a promise is pretty simple because it is an alias for then(null, errorCallback).
155+
156+
//Traps of Catch
157+
158+
// var promise = request();
159+
160+
// promise.catch(function(error) {
161+
// displayError(error);
162+
// });
163+
164+
// // This is the same code as this:
165+
// promise.then(null, function(error) {
166+
// displayError(error);
167+
// });
168+
169+
// Example 1
170+
171+
// let promise = request();
172+
173+
// promise.then(function(data) {
174+
// console.log(data);
175+
// }, function(error) {
176+
// console.error(error);
177+
// });
178+
179+
// Example 2
180+
181+
// let promise = request();
182+
183+
// promise
184+
185+
// .then(function(data) {
186+
// console.log(data);
187+
// })
188+
189+
// .catch(function(error) {
190+
// console.error(data);
191+
// });
192+
193+
// To understand how to use then and catch directly, you can think of it as
194+
// pretty similar to using try { ... } catch { ... }.
195+
// Let's take the 2 examples and express them in "pseudo-synchronous code"
196+
197+
//Example 1
198+
199+
// try {
200+
// let promise = request();
201+
// } catch (error) {
202+
// console.error(error);
203+
// return;
204+
// }
205+
// console.log(data);
206+
207+
// Example 2
208+
209+
// try {
210+
// let promise = request();
211+
// console.log(data);
212+
// } catch (error) {
213+
// console.error(error);
214+
// }
215+
216+
//Try catch Promise
217+
218+
// function job() {
219+
// return new Promise(function(resolve, reject) {
220+
// reject();
221+
// });
222+
// }
223+
224+
// let promise = job();
225+
226+
// promise
227+
228+
// .then(function() {
229+
// console.log('Success 1');
230+
// })
231+
232+
// .then(function() {
233+
// console.log('Success 2');
234+
// })
235+
236+
// .then(function() {
237+
// console.log('Success 3');
238+
// })
239+
240+
// .catch(function() {
241+
// console.log('Error 1');
242+
// })
243+
244+
// .then(function() {
245+
// console.log('Success 4');
246+
// });
247+
248+
//Another Example
249+
250+
// function job(state) {
251+
// return new Promise(function(resolve, reject) {
252+
// if (state) {
253+
// resolve('success');
254+
// } else {
255+
// reject('error');
256+
// }
257+
// });
258+
// }
259+
260+
// let promise = job(true);
261+
262+
// promise
263+
264+
// .then(function(data) {
265+
// console.log(data);
266+
267+
// return job(false);
268+
// })
269+
270+
// .catch(function(error) {
271+
// console.log(error);
272+
273+
// return 'Error caught';
274+
// })
275+
276+
// .then(function(data) {
277+
// console.log(data);
278+
279+
// return job(true);
280+
// })
281+
282+
// .catch(function(error) {
283+
// console.log(error);
284+
// });
285+
286+
//Another Example
287+
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+
// })
319+
320+
// .catch(function (error) {
321+
// console.log(error);
322+
323+
// return job(false);
324+
// })
325+
326+
// .then(function (data) {
327+
// console.log(data);
328+
329+
// return job(true);
330+
// })
331+
332+
// .catch(function (error) {
333+
// console.log(error);
334+
335+
// return "Error caught";
336+
// })
337+
338+
// .then(function (data) {
339+
// console.log(data);
340+
341+
// return new Error("test");
342+
// })
343+
344+
// .then(function (data) {
345+
// console.log("Success:", data.message);
346+
// })
347+
348+
// .catch(function (data) {
349+
// console.log("Error:", data.message);
350+
// });
351+
352+
//Promise.All , Incase of Multiple Asynchronous tasks have to do with single promise.
353+
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+
// }
362+
363+
// var promise = Promise.all([job(1000), job(2000), job(500), job(1500)]);
364+
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
373+
374+
// function delay(time) {
375+
// return new Promise(function(resolve) {
376+
// setTimeout(resolve, time, 'success ' + time);
377+
// });
378+
// }
379+
380+
// Promise.race([delay(500), delay(100)]).then(function(data) {
381+
// console.log(data);
382+
// });
383+

0 commit comments

Comments
 (0)