How to return the value of data.reponse [0] .uid from the test function?

function test(targetUid){ VK.api("users.get", {uids:shortName,fields:"photo_50"}, function(data) { return data.response[0].uid; }); } var nick = "123123"; console.log(test(nick)); 

Reported as a duplicate by user207618, Pavel Mayorov , Grundy , tutankhamun , D-side 29 Aug '16 at 19:44 .

A similar question was asked earlier and an answer has already been received. If the answers provided are not exhaustive, please ask a new question .

  • 2
    Oh no, again ... :) - Pavel Mayorov
  • @PavelMayorov, whether it will still be ... - user207618

1 answer 1

like this:

 function test(targetUid, dataObject, onDataRecieved) { VK.api("users.get", {uids:shortName,fields:"photo_50"}, function(data) { onDataRecieved(dataObject, data.response[0].uid); }); } function onNickDataRecieved(dataObject, data) { dataObject.result = data; } var nick = "123123", nickData = {}; test(nick, nickData, onNickDataRecieved); nickData.result && console.log(nickData.result); 

but just read about the promises - they offer a more convenient interface for working with asynchronous code