Menu

gitpiper

promisify javascript Code Snippet in 2023

functionpromiseintermediate

Last Updated: 28 May 2023

Converts an asynchronous function to return a promise.

  • Use currying to return a function returning a Promise that calls the original function.
  • Use the rest operator (...) to pass in all the parameters.
  • Note: In Node 8+, you can use util.promisify.
const promisify = func => (...args) => new Promise((resolve, reject) => func(...args, (err, result) => (err ? reject(err) : resolve(result))) );
const delay = promisify((d, cb) => setTimeout(cb, d)); delay(2000).then(() => console.log('Hi!')); // Promise resolves after 2s

javascript snippet similar to promisify For You in May 2023

Subscribe to our Newsletter

Subscribe to get resources directly to your inbox. You won't receive any spam! ✌️

© 2023 GitPiper. All rights reserved

Rackpiper Technology Inc

Company

About UsBlogContact

Subscribe to our Newsletter

Subscribe to get resources directly to your inbox. You won't receive any spam! ✌️