Git Product home page Git Product logo

Comments (4)

Elbarae1921 avatar Elbarae1921 commented on July 21, 2024 2

a callback is basically a function that takes another function as argument, and then calls that function at some point.
Here's an example

function callbackFunction(cb) {
  doSomething();
  cb()
} 

/*----------------------------------------*/

callbackFunction(() => console.log('finished'));

A promise on the other hand is different as you can await it.

const myPromise = return new Promise(resolve => {
  doSomething();
  resolve();
});

// OR

async function myPromise() {
  doSomething();
}

/*----------------------------------------*/

async function main() {
  await myPromise();
  console.log('finished');
}

// OR you can use the then syntax

myPromise().then(() => console.log('finished'));

This is how you would promisify a callback

function callbackFunction(cb) {
  doSomething();
  cb()
} 

const promisifiedCallback = new Promise(res => {
  callbackFunction(() => {
    resolve();
  });
});

// then instead of having to do

function main() {
  callbackFunction(() => {
    // shove all the rest of the main function code here
  });
}

// you can

async function main() {
  await promisifiedCallback();
  // the rest of the main function code
}

from lireddit.

Gerwyn1 avatar Gerwyn1 commented on July 21, 2024

@Elbarae1921 honestly i still don't really get it ☚ī¸

from lireddit.

Elbarae1921 avatar Elbarae1921 commented on July 21, 2024

@Gerwyn1 Hey, which part exactly do you not get?

from lireddit.

Gerwyn1 avatar Gerwyn1 commented on July 21, 2024

@Elbarae1921 the differences that you are trying to showcase. i can't tell the huge difference

from lireddit.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤ī¸ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.