How to return the value within a function?

Asked

Viewed 133 times

1

What’s the right way to make one return in a Function response, I have the following code:

$('random-button').click(function(){
    console.log(moveFile());
});

function moveFile(fileID, newFolderID, currentFolder){
     gapi.client.drive.files.update({
        'fileId': fileID,
        'addParents': newFolderID,
        'removeParents' : currentFolder

    }).execute(function(resposta){
        return resposta;
    });
}

I need to send back the value obtained as a response, I tried to save the whole function in a variable however the return comes as undefined, how can I do it?

  • In that case, I think it’s a lack of timing. When calling the function in the console.log, the function does not have a ready answer, it will still process something, but the function call does not wait and returns "Undefined".

  • 1

    Hi Leo! Take a look at the answers I’ve gathered. If you still can’t solve/understand the problem, it says that part is not clear.

  • Thanks @Sergio the first solution works well for me, although I thought there was a more elegant solution, I’ve seen this type of use before and I thought it was gambiarra.

  • In such cases, asynchronous, or chained inside the callback or else using async/await, there is no other way to use var x = respostaDaCallback;

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.