I have got a javascript code like this:
function justTesting() {
promise.then(function(output) {
return output + 1;
});
}
var test = justTesting();
I have got always an undefined value for the var test. I think that it is because the promises are not resolved yet..there is a way to return a value from a promise?
This question is related to
javascript
promise
I prefer to use "await" command and async functions to get rid of confusions of promises,
In this case I would write an asynchronous function first, this will be used instead of the anonymous function called under "promise.then" part of this question :
async function SubFunction(output){
// Call to database , returns a promise, like an Ajax call etc :
const response = await axios.get( GetApiHost() + '/api/some_endpoint')
// Return :
return response;
}
and then I would call this function from main function :
async function justTesting() {
const lv_result = await SubFunction(output);
return lv_result + 1;
}
Noting that I returned both main function and sub function to async functions here.
You cannot return value after resolving promise. Instead call another function when promise is resolved:
function justTesting() {
promise.then(function(output) {
// instead of return call another function
afterResolve(output + 1);
});
}
function afterResolve(result) {
// do something with result
}
var test = justTesting();
When you return something from a then()
callback, it's a bit magic. If you return a value, the next then()
is called with that value. However, if you return something promise-like, the next then()
waits on it, and is only called when that promise settles (succeeds/fails).
Source: https://web.dev/promises/#queuing-asynchronous-actions
To use a promise, you have to either call a function that creates a promise or you have to create one yourself. You don't really describe what problem you're really trying to solve, but here's how you would create a promise yourself:
function justTesting(input) {_x000D_
return new Promise(function(resolve, reject) {_x000D_
// some async operation here_x000D_
setTimeout(function() {_x000D_
// resolve the promise with some value_x000D_
resolve(input + 10);_x000D_
}, 500);_x000D_
});_x000D_
}_x000D_
_x000D_
justTesting(29).then(function(val) {_x000D_
// you access the value from the promise here_x000D_
log(val);_x000D_
});_x000D_
_x000D_
// display output in snippet_x000D_
function log(x) {_x000D_
document.write(x);_x000D_
}
_x000D_
Or, if you already have a function that returns a promise, you can use that function and return its promise:
// function that returns a promise_x000D_
function delay(t) {_x000D_
return new Promise(function(resolve) {_x000D_
setTimeout(function() {_x000D_
resolve();_x000D_
}, t);_x000D_
});_x000D_
}_x000D_
_x000D_
function justTesting(input) {_x000D_
return delay(100).then(function() {_x000D_
return input + 10;_x000D_
});_x000D_
}_x000D_
_x000D_
justTesting(29).then(function(val) {_x000D_
// you access the value from the promise here_x000D_
log(val);_x000D_
});_x000D_
_x000D_
// display output in snippet_x000D_
function log(x) {_x000D_
document.write(x);_x000D_
}
_x000D_
Promises
don't "return" values, they pass them to a callback (which you supply with .then()).
It's probably trying to say that you're supposed to do resolve(someObject);
inside the promise implementation.
Then in your then
code you can reference someObject
to do what you want.
What I have done here is that I have returned a promise from the justTesting function. You can then get the result when the function is resolved.
// new answer
function justTesting() {
return new Promise((resolve, reject) => {
if (true) {
return resolve("testing");
} else {
return reject("promise failed");
}
});
}
justTesting()
.then(res => {
let test = res;
// do something with the output :)
})
.catch(err => {
console.log(err);
});
Hope this helps!
// old answer
function justTesting() {
return promise.then(function(output) {
return output + 1;
});
}
justTesting().then((res) => {
var test = res;
// do something with the output :)
}
Source: Stackoverflow.com