Chrome Dev tools - javascript snippet test : “Uncaught Syntax Unexpected End-of-input” error on last line

51
June 11, 2018, at 06:50 AM

I am trying to test the following snippet using the Chrome Dev Tool, but I get this error on the last line of the snippet ... cannot find where it's wrong ...

function myPromise1(time1, ok1){
  return new Promise((resolve, reject) => {
    // do stuff 1 async
    setTimeout(() => {
      if ( ok1 ) {
        resolve('stuff-1 worked')
      } else {
        reject(Error('stuff-1 failed'))
      }
    }, time1)
  });
  function myPromise2(time2, ok2){
    return new Promise((resolve, reject) => {
      // do stuff 2 async
      setTimeout(() => {
        if ( ok2 ) {
          resolve('stuff-2 worked')
        } else {
          reject(Error('stuff-2 failed'))
        }
      }, time2)
    });
    function myPromise3(time3, ok3){
      return new Promise((resolve, reject) => {
        // do stuff 3 async
        setTimeout(() => {
          if ( ok3 ) {
            resolve('stuff-3 worked')
          } else {
            reject(Error('stuff-3 failed'))
          }
        }, time3)
      });
      const c1 = '';
      const c2 = '';
      const c3 = '';
      const promise1 = (val11, val12) => myPromise1(val11, val12)
      .then(p1Result => {
        c1 = p1Result;
        return;
      }, function(err1) {
        console.log('Error: ', err1);
        return;
      });
      const promise2 = (val21, val22) => myPromise2(val21, val22)
      .then((p2Result) => {
        c2 = p2Result;
        return;
      }, function(err2) {
        console.log('Error: ', err2);
        return;
      });
      const promise3 = (val31, val32) => myPromise2(val31, val32)
      .then((p3Result) => {
        c3 = p3Result;
        return;
      }, function(err3) {
        console.log('Error: ', err3);
        return;
      });
      const conditionalPromiseFlow = (...fns) => {
        if(fns.length === 0) return Promise.resolve()
        const [next] = fns;
        return next()
          .then(result => {
          if(result) {
            return conditional(...fns.slice(1));
          }
          return result;
        })
      }
      conditionalPromiseFlow(() => promise1(1000, true), () => promise2(2000, true), () => promise3(3000, true))
        .then(() => {
        console.log('Status 200 - ALL DONE');
        return;
      })
        .catch((error) => {
        console.log('Status ', error.status, ' - ', error.message);
        return;
      })  <== Error stated here
Answer 1

this is the corrected snippets to test promises flow... I can play with the main() line

conditionalPromiseFlow(() => promise1(1000, true), () => promise2(2000, true), () => promise3(3000, true))

the flow stops when one promise is rejected

function myPromise1(time1, ok1){
    console.log('Promise1', time1, ok1);
    return new Promise((resolve, reject) => {
        setTimeout(() => {
            if ( ok1 ) {
                resolve('stuff-1 worked');
            } else {
                reject(Error('stuff-1 failed'));
            }
        }, time1);
    });
}
function myPromise2(time2, ok2){
  console.log('Promise2', time2, ok2);
    return new Promise((resolve, reject) => {
        setTimeout(() => {
            if ( ok2 ) {
                resolve('stuff-2 worked');
            } else {
                reject(Error('stuff-2 failed'));
            }
        }, time2);
    });
}
function myPromise3(time3, ok3){
  console.log('Promise3', time3, ok3);
    return new Promise((resolve, reject) => {
        setTimeout(() => {
            if ( ok3 ) {
                resolve('stuff-3 worked');
            } else {
                reject(Error('stuff-3 failed'));
            }
        }, time3);
    });
}
let c1 = '';
let c2 = '';
let c3 = '';
const promise1 = (val11, val12) => myPromise1(val11, val12)
    .then(p1Result => {
      c1 = p1Result;
      return;
    }, function(err1) {
        err1.status = 300;
        throw err1;
    });
const promise2 = (val21, val22) => myPromise2(val21, val22)
    .then((p2Result) => {
      c2 = p2Result;
      return;
    }, function(err2) {
        err2.status = 300;
        throw err2;
    });
const promise3 = (val31, val32) => myPromise3(val31, val32)
    .then((p3Result) => {
      c3 = p3Result;
      return;
    }, function(err3) {
        err3.status = 300;
        throw err3;
    });
const conditionalPromiseFlow = (...fns) => {
  if(fns.length === 0) return Promise.resolve();
  const [next] = fns;
  return next()
    .then(result => {
      if(!result) {
        return conditionalPromiseFlow(...fns.slice(1));
      }
      return result;
    });
};
conditionalPromiseFlow(() => promise1(1000, true), () => promise2(2000, true), () => promise3(3000, true))
 .then(() => {
    console.log('Status 200 - ALL DONE');
    return;
  })
 .catch((error) => {
    console.log('Status ', error.status, ' - ', error.message);
    return;
  });
READ ALSO
Building mutli dimensional array filter with es6

Building mutli dimensional array filter with es6

As shown in the Screenshot im trying to build a Filter logic which cannot be handeled in backend/sql for other reasons

47
How i can wait for image data url?

How i can wait for image data url?

I want to use image when readyMy code works, but not every time

58
ejs won&#39;t render my images with Nodejs

ejs won't render my images with Nodejs

I'm trying to render an ejs file using NodeJS WITHOUT ExpressI have the following code:

82
How to scroll text in Javascript for specific characters

How to scroll text in Javascript for specific characters

As per requirement, I need to scroll horizontal text with specific size consider I have text with 60 character and display area size is 40 then result should be

73