3

Is it possible to return a response when call a function within another function?

I have following codes -

// Add Category
exports.saveCategory = async function(catData, callback){

    try{
        const images = await uploadImages(catData.files);
        console.log(images); //nothing prints here
        const save = await function(catData,images){
            console.log('catdata');
            return callback({status:200,message:'test'});
        }
    } catch (e) {
        console.log(e);
    }
}

function uploadImages(images) {
    //some stuff
    return images;
}

Expected output : I want to return name of uploaded images from uploadImages function and pass it to another function to save in database.

2 Answers 2

4

Only async function returns promise.Make your uploadImages function async

async function uploadImages(images) {
        //some stuff
        return images;
    }
Sign up to request clarification or add additional context in comments.

Comments

0

The solution of Shubh Dixit works fine but i have to correct, that async functions do NOT return a real Promise but async function object. These do not have a .catch() or .finally() method. He is right, that a return inside a async function object will return a Promise.resolve()

async function x() {
    return 2;
}
let value = await x();

returns a solved Promise with the value 2. Then the await returns the value of the resolved Promise to value. But if you try

let value = await x().catch((error)=> {console.log(error);});

you will recieve an error that .catch() is no method of x.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.