Can An Async Function Return Undefined Instead Of A Promise
I am working on an app using nodejs. I am making multiple HTTP requests with an async function and axios library. However I do not always want to return the fetched data from my http request, only if a certain condition is met.
Like so.
const getFooHTTP = async (id) => {
let response = await axios.get(url);
if (condition){
//I only want to return the response here
return response;
}
//Here i do not want to return the response
}
Then I am getting all the promises returned in an array with Promise.all()
const getAllData = async() => {
let dataArray = [];
for (let i = 0; i < n; i++){
const data = getFooHTTP(i);
dataArray.push(data)
}
const someData = await Promise.all(dataArray);
return someData ;
}
Then I get all the data
getAllData().then(data => {
//Here is the problem, here I get a bunch of undefined in my data array
console.log(data);
})
Here is my problem, when I get the returned data from getAllData
, there is some undefined element because at the first function in the beginning (getFooHTTP
) was returning nothing. My question is how can I return promises conditionally, so I don't get undefined promises returned even If the async function have no return statement.
Thank you
Answer
An async
function will always return a Promise, no matter what. If you explicitly return a non-Promise even if there are no await
s before it, it will be automatically wrapped in a Promise before returning (eg return undefined
will turn into something like return Promise.resolve(undefined)
).
const prom = (async () => {
return undefined;
})();
// Even though it returned undefined, it's still a Promise:
console.log(typeof prom.then);
If you don't want to return values that don't fulfill condition
, filter
the Promise.all
before returning it:
const getFooHTTP = async (id) => {
let response = await axios.get(url);
if (condition){
//I only want to return the response here
return response;
}
//Here i do not want to return the response
return undefined;
// or, have no return statement at all
};
and
const getAllData = async() => {
let dataArray = [];
for (let i = 0; i < n; i++){
const data = getFooHTTP(i);
dataArray.push(data)
}
const someData = (await Promise.all(dataArray))
.filter(val => val !== undefined);
return someData ;
};
Though, this relies on all of the Promises that getFooHTTP
resolves to returning non-undefined
values.
Related Questions
- → How to update data attribute on Ajax complete
- → October CMS - Radio Button Ajax Click Twice in a Row Causes Content to disappear
- → Octobercms Component Unique id (Twig & Javascript)
- → Passing a JS var from AJAX response to Twig
- → Laravel {!! Form::open() !!} doesn't work within AngularJS
- → DropzoneJS & Laravel - Output form validation errors
- → Import statement and Babel
- → Uncaught TypeError: Cannot read property '__SECRET_DOM_DO_NOT_USE_OR_YOU_WILL_BE_FIRED' of undefined
- → React-router: Passing props to children
- → ListView.DataSource looping data for React Native
- → Can't test submit handler in React component
- → React + Flux - How to avoid global variable
- → Webpack, React & Babel, not rendering DOM