Skip to content

Commit 35fc46a

Browse files
committed
promisifying-setTimeout
1 parent 1de7158 commit 35fc46a

File tree

1 file changed

+33
-1
lines changed
  • 16 Asynchronous JavaScript_ Promises, Async_Await, and AJAX

1 file changed

+33
-1
lines changed

16 Asynchronous JavaScript_ Promises, Async_Await, and AJAX/script.js

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,8 @@ const whereAmI = function (lat, lng) {
224224
225225
whereAmI(52.508, 13.381);
226226
*/
227-
227+
////--=======---////
228+
/*
228229
setTimeout(() => console.log('0 sec timer'), 0);
229230
Promise.resolve('Resolved promise 1').then(res => console.log(res));
230231
@@ -234,3 +235,34 @@ Promise.resolve('Resolved promise 2').then(res => {
234235
}
235236
});
236237
console.log('Test end');
238+
*/
239+
/////---=======-----////////
240+
const lotteryPromise = new Promise(function (resolve, reject) {
241+
console.log('Lottery draw is happening');
242+
setTimeout(function () {
243+
if (Math.random() >= 0.5) {
244+
resolve('You Win');
245+
} else {
246+
reject('You lost your money');
247+
}
248+
}, 2000);
249+
});
250+
251+
lotteryPromise.then(res => console.log(res)).catch(err => console.error(err));
252+
253+
// --=== Promisifying setTimeout ---==== ////
254+
const wait = function (seconds) {
255+
return new Promise(function (resolve) {
256+
setTimeout(resolve, seconds * 1000);
257+
});
258+
};
259+
260+
wait(2)
261+
.then(() => {
262+
console.log('I waited for 2 seconds');
263+
return wait(1);
264+
})
265+
.then(() => console.log('I waited for 1 second'));
266+
267+
Promise.resolve('abc').then(x => console.log(x));
268+
Promise.reject(new Error('Problem!')).catch(x => console.log(x));

0 commit comments

Comments
 (0)