Javascript difference between promise in function and var equal to promise


Javascript difference between promise in function and var equal to promise



I am trying to work on some promises and I'm a bit confused on the difference between the below 2 ways of using promises:



First method:


function myfunction() {
return new Promise((resolve, reject) => {
// do stuff
});
}



second method:


var myfunction = new Promise((resolve, reject) => {
// do stuff
});



What is the difference between these two?



Is there preference on when to use one or the other?





One is a promise, the other is a function. You use the promise when you need a promise, and you use the function when you need a function. they aren't directly interchangeable.
– Kevin B
2 days ago






No. One is a promise, the other will create and return a promise if/when it is called.
– Kevin B
2 days ago






var myfunction is a variable that holds a Promise, so it might be less confusing if you say var mypromise
– Kokodoko
2 days ago


var myfunction


var mypromise





(assuming you change the var mypromise to var myfunction) in Promise.all you'd use something like [myFunction(), myPromise] ... the difference being the do stuff in myFunction won't be done until myFunction() is called - whereas the do stuff in myPromise will have already been done when the var was defined
– Jaromanda X
2 days ago



Promise.all


[myFunction(), myPromise]


do stuff


myFunction()


do stuff


myPromise





Same difference between these var x = 1; and var x = function() { return 1;}. You have to call the function to get the value.
– jfriend00
2 days ago


var x = 1;


var x = function() { return 1;}




1 Answer
1



In a nutshell, your two examples are like comparing these two statements:


var x = 1;

var x = function() { return 1;}



The first defines a variable and assigns a value to it.
The second defines a variable and assigns a function to it. That function must be called before it does anything or produces any specific value.



Your first code example:


function myfunction() {
return new Promise((resolve, reject) => {
// do stuff
});
}



Is a function that creates and returns a new promise. when it is called. You have to call the function before it does anything. The code as you show it just defines a function, but doesn't actually run anything. myfunction is a function. To get the promise, you must call that function as in:


myfunction


myfunction().then(...).catch(...)



Your second code example:


var myfunction = new Promise((resolve, reject) => {
// do stuff
});



creates a new promise and assigns the promise itself to your variable myfunction. After this code runs, myfunction will contain a promise.


myfunction


myfunction



So, in the first example myfunction is a function. In the second example, myfunction is a promise.


myfunction


myfunction



What is the difference between these two?



As described above, they are different pieces of code for different purposes.



Is there preference on when to use one or the other?



It depends upon what you want to do. If you want to define a reusable function that you can call more than once that creates a promise (that presumably tracks some asynchronous operation), then you would use the first one and you would call it each time you wanted to run the async operation and get the promise back.



If you want to (just this once) create a promise and put it in a variable you would use the second one. Different constructions of code for different purposes that do different things.



Keep in mind that a promise is just an ordinary Javascript object that serves as a notification system for tracking and notifying other code about the completion of an asynchronous operation. When you do new Promise() all that happens is a new Javascript object is created. Promises are just plain Javascript objects with a few methods and some instance data. They have no magic powers to observe anything. In fact, they only do exactly what they're told. Some people think that they somehow automatically know when an asynchronous operation inside of them is done. They do not. You have to manually hook into the completion operation of the asynchronous operation and manually call resolve(...) or reject(...). When you do, the promise will then notify any listeners that have installed themselves with .then(...) or .catch(...).


new Promise()


resolve(...)


reject(...)


.then(...)


.catch(...)






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Comments

Popular posts from this blog

paramiko-expect timeout is happening after executing the command

Export result set on Dbeaver to CSV

Opening a url is failing in Swift