Snippets Collections
console.log('Hi')

const func = () => {
    return new Promise(resolve => {
        setTimeout(() => {
            console.log('2nd Hi')
            resolve();
        }, 2000)
    })
}

(async () => {
    await func();
    console.log('3rd Hi');
})();
const sleep = (delay) => new Promise((resolve) => setTimeout(resolve, delay))

const sumWithDelay = async (a, b) => {
  console.log("Delay started")
  await sleep(3000)
  console.log("Delay ended")
  return a + b;
}

const sum = async (a, b) => {
  return a + b;
}

const mul = async (a, b) => {
  return a * b;
}

let dne = async function() {
  return 'it does not exist';
};

let doCalculation = function(route) {
  switch (route) {
    case 'sum':
      return sum;
    case 'sumWithDelay':
      return sumWithDelay;
    case 'mul':
      return mul;
    default:
      return dne;
  }
};

doCalculation('sumWithDelay')(2, 2).then(res => console.log(res)); //4
doCalculation('sum')(3, 4).then(res => console.log(res)); // 7
doCalculation('mul')(3, 4).then(res => console.log(res)); // 12
doCalculation('dne')(3, 4).then(res => console.log(res)); // it does not exist
 process.on('unhandledRejection', (error) => {
    throw error;
  });

  process.on('uncaughtException', (error) => {
    logError(error);

    if (!isOperationalError(error)) {
      process.exit(1);
    }
  });
function promiseWrapper(fn) {
    return (req, res, next) => {
         fn(req, res).catch(next);
    };
}
var url = "http://scratch99.com/web-development/javascript/";
var urlParts = url.replace('http://','').replace('https://','').split(/[/?#]/);
var domain = urlParts[0];
star

Fri Mar 01 2024 18:24:04 GMT+0000 (Coordinated Universal Time)

#javascript #promises
star

Tue Feb 22 2022 14:02:19 GMT+0000 (Coordinated Universal Time) https://stackoverflow.com/questions/65599362/simple-callback-to-promise-conversion

#javascript #promises
star

Tue Aug 24 2021 16:30:12 GMT+0000 (Coordinated Universal Time) https://sematext.com/blog/node-js-error-handling/

#nodejs #promises #unhandled
star

Sun Jun 06 2021 15:34:43 GMT+0000 (Coordinated Universal Time)

#nodejs #async #promises #express #javas
star

Fri Jan 10 2020 22:36:50 GMT+0000 (Coordinated Universal Time) http://scratch99.com/web-development/javascript/how-to-get-the-domain-from-a-url/

#javascript #promises #howto

Save snippets that work with our extensions

Available in the Chrome Web Store Get Firefox Add-on Get VS Code extension