Quick actions

cmd+k|ctrl+k

Navigation

Languages

Use case of promises

Snippet info

Language

JavaScript

Visibility

public

Author

pranavchaitu676

Created

2024-01-29T15:59:26.133088Z

Updated

2024-01-29T15:59:26.133088Z

// my own asynchronous function

function pranav(cb) {
  console.log("in the pranavfunction");
  setTimeout(function(){
    cb("This is a callback function");
  },1000)
}

function logIt(data) {
  console.log(data)
}

pranav(logIt);


//Promises
//just the sintactical sugar for the callback
//no need of callbacks
//but works by callbacks under the hood

function pranav() {
  return new Promise(function(resolve) {
    setTimeout(function() {
      resolve("this returns by the promise");
    },1000)
  })
}

function logIt(data) {
  console.log(data)
}

console.log("before promise")
pranav().then(logIt);
console.log("after promise")


INFO