Calling Promise.resolve() on *potentially* non-promise values [duplicate] - javascript

This question already has answers here:
How to handle functions that do not always return a promise?
(2 answers)
Closed 4 years ago.
I have a situation where I need to check if a function returns a promise, the problem is, its a browser implemented function and some browsers DO return a promise and some don't. I was checking: How do I tell if an object is a Promise?, to see how to check if a function returns a promise and it recommends using Promise.resolve(). But what exactly happens when you call Promise.resolve() on a non-promisified function?
I tried reading this, but i wasn't able to find an answer to the question exactly: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/resolve

From the MDN documentation:
returns a Promise object that is resolved with the given value. If the value is a promise, that promise is returned; if the value is a thenable (i.e. has a "then" method), the returned promise will "follow" that thenable.
So yeah wrapping that returned value with Promise.resolve would take care of promisifying that value.

You can assign what the function returns to a variable and then evaluate that variable like so:
var obj = whateverFunction();
if(typeof obj.then === "function"){
// This is a promise because it has a then function
}
Although I suspect the answer using Promise.resolve might be better.

Related

How can I return a value from .then method of a promise? [duplicate]

This question already has answers here:
Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference
(7 answers)
How do I return the response from an asynchronous call?
(41 answers)
Closed 2 years ago.
In the following javascript code snippet (written for nodejs), the createDoc function returns a promise. Upon fulfillment of the promise, the handler passed to .then is invoked. The question is that how can I make .then return doc? According to the documentation, .then returns a promise but I'm really looking for it to return doc.
let p = createDoc(title).then(function (doc) {return doc;});
//p is a promise, and not doc
console.log(p);
What is the proper way to access the value passed to the fulfillment handler (doc in this case) outside the .then?
do something like this and when the promise resolves the value of doc will be inside p
let p;
createDoc(title).then(doc => p = doc);
You can use async-await, if you want to have the value instead of promise in p
async function someFunction() {
...
let p = await createDoc(title);
console.log(p);
...
}
What you are seeing is a behavior of promises calling chaining.

Javascript return Promise from function - promise chain vs variable [duplicate]

This question already has an answer here:
Is there a difference between promise.then.then vs promise.then; promise.then [duplicate]
(1 answer)
Closed 5 years ago.
Is there a difference between these two ways for returning promise from a function
var promise;
promise = callAsync();
promise.then(doSomething).then(doSomethingElse);
return promise;
vs
var promise;
promise = callAsync();
return promise.then(doSomething).then(doSomethingElse);
I thought both approaches are same, but in a mocha test case, only second approach is working.
They are certainly not the same. Each .then() returns a new promise. So,
return promise;
is returning the original promise, but:
return promise.then(doSomething).then(doSomethingElse);
is returning a new promise that is the result of calling both .then() methods. The difference is that the latter one is returning a promise that is influenced by the functions called in those .then() methods, whereas the first promise is only influence by callAsync() and has nothing to do with the other .then() handlers.
The first promise is only monitoring callAsync(). It has absolutely nothing to do with what happens in other .then() handlers. The key to understanding .then() is that it returns a new promise and it is that new promise that is influenced by what happens in the .then() handlers.
For more info, read this answer: Is there a difference between promise.then.then vs promise.then; promise.then to understand the difference between chaining and branching.

Is there a simple way to determine if Protractor is returning a promise? [duplicate]

This question already has an answer here:
Checking if an object is a promising function
(1 answer)
Closed 7 years ago.
I am hoping for any quick way (hopefully one that I can use in many/all instances) to verify that a selector is returning a promise and not an element.
A hack solution might be something like:
var result = something.getSomething(someArgs);
if(result.then){
// it's a promise.
}else{
// the usual stuff.
}
the right way might be using instanceof, I believe protractor promises are of type webdriver.promise.Promise( would like some confirmation on this point), a simple checking method might be:
function isPromise(obj){
return obj instanceof webdriver.promise.Promise || webdriver.promise.isPromise(obj);
}
//usage
var result = something.getSomething(someArgs);
if(isPromise(result))){
// it's a promise.
}else{
// the usual stuff.
}
Your best bet if you are not sure if something is a promise or not is to use promise.when() to turn it into a promise if it is not already one, and thus you can treat the result as a promise safely.
But most things in protractor seem to return promises that I have seen, so this should be a redundant operation in most cases.

calling a async function inside then [duplicate]

This question already has answers here:
Removing nested promises
(2 answers)
Closed 7 years ago.
I have a piece of code that looks like this:
func().then(function (result){
var a = func1(result);
func2(a).then(function(result1){
////
}
}
As you can see func returns a promise, and in then part we call another func1 which also returns a promise. Is it possible to chain the promise returned from func2 with the promise of then, and somehow get ride of the nested functions in the second then.
The return value inside a then() function is used as a promise value itself. So you can easily return the new promise and keep on chaining here:
func()
.then(function (result){
var a = func1(result);
return func2(a);
})
.then(function(result1){
////
})
See 2.2.7 and 2.3.2 of the Promise A+ Spec.

How to make a callback that gets executed when all the promises have settled? [duplicate]

This question already has answers here:
$.Deferred: How to detect when every promise has been executed
(3 answers)
Closed 9 years ago.
I am using jquery for promises.
I have a scenario, where i have two promises.
If promise1 rejects or resolve then system1 should reject or resolve respectively.
If promise2 rejects or resolve, system2 should resolve.
Function X should be called after both are settled and both system1 and system2 has resolved.
I tried :
var dp = $.when(promise1, promise2);
dp.done(function(one,two){
X();
}).fail(function(){
// promise1 might have not settled as of yet.
Should call X or not ?
});
But it returns as soon as one of the promise fails. So my promise1 is not resolved at the time when my fail is called.
How do i do it ?
The behaviour is exactly as per the documentation: The answer for your question is also in the documentation as italised.
In the multiple-Deferreds case where one of the Deferreds is rejected,
jQuery.when immediately fires the failCallbacks for its master
Deferred. Note that some of the Deferreds may still be unresolved at
that point. If you need to perform additional processing for this
case, such as canceling any unfinished ajax requests, you can keep
references to the underlying jqXHR objects in a closure and
inspect/cancel them in the failCallback.

Categories

Resources