QNimate

  • CoursesVideos
  • WP PremiumPlugins
  • DemosLab
  • Bitcoin BankingBuy, Sell, Store and Earn Interest
  • Home
  • QIdea
  • QTrack
Home Carbon Ads JavaScript Promise vs Callback

JavaScript Promise vs Callback

A callback is a function that is passed to an another function. A callback may or may not be executed asynchronously. For example;

function two()
{
    return 2;
}

function x(y)
{
    //execute y
    return y();
}

console.log(x(two)); //2

Here two() is a function. We are passing it as callback to function x(). Function x() may or may not execute it asynchronously. Here callback is executed asynchronously.

A Promise is a object which takes a callback and executes it asynchronously. For example;

function two()
{
    return 2;
}
 
function error(e)
{
    console.log(e);
}

var promise = new Promise(function(callback, e){
    console.log(callback());
});

promise.then(two, error);

Here we passed two() as callback to promise’s then() function which is indeed executed asynchronously.

Feb 21, 2015Narayan Prusty
Difference between "Map" and "WeakMap" in JavaScriptChange Position of WordPress Dashboard Widget

Leave a Reply Cancel reply

To create code blocks or other preformatted text, indent by four spaces:

    This will be displayed in a monospaced font. The first four
    spaces will be stripped off, but all other whitespace
    will be preserved.
    
    Markdown is turned off in code blocks:
     [This is not a link](http://example.com)

To create not a block, but an inline code span, use backticks:

Here is some inline `code`.

Narayan Prusty

I am a full-stack web developer. I specialize in Blockchain and JavaScript. This is my personal blog where I write about programming and technologies that I learn and feel interesting to share.

6 years ago Web Development
Share this
0
GooglePlus
0
Facebook
0
Twitter
0
Linkedin
Related Articles
  • JavaScript Arrow “=>” Function
  • JSON.parse() throws “unexpected token” error for valid JSON
  • Generate Unique Number in JavaScript
  • JavaScript Limit Function Call Rate
  • Javascript “use strict” Tutorial with Examples
Our Sponsor
Freelance: I am available
@narayanprusty
Hi, I am a full-stack developer - focusing on JavaScript, WordPress, Blockchain, DevOps, Serverless and Cordova. You can outsource your app development to me.





Will get back to you soon!!!
WordPress
ReactJS
Meteor
Blockchain
Serverless
Kubernetes
DevOps
DB & Storage
Ejabberd
Let's get started
My Books

2014 - 2015 © QNimate
All tutorials MIT license