File tree Expand file tree Collapse file tree 1 file changed +33
-1
lines changed
16 Asynchronous JavaScript_ Promises, Async_Await, and AJAX Expand file tree Collapse file tree 1 file changed +33
-1
lines changed Original file line number Diff line number Diff line change @@ -224,7 +224,8 @@ const whereAmI = function (lat, lng) {
224224
225225whereAmI(52.508, 13.381);
226226*/
227-
227+ ////--=======---////
228+ /*
228229setTimeout(() => console.log('0 sec timer'), 0);
229230Promise.resolve('Resolved promise 1').then(res => console.log(res));
230231
@@ -234,3 +235,34 @@ Promise.resolve('Resolved promise 2').then(res => {
234235 }
235236});
236237console.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 ) ) ;
You can’t perform that action at this time.
0 commit comments