0

I have been struggling for quiet sometime now with conversion of file chunks to binary data and have come to this point

for(....){
    $.when(chunkBinary(chunk[i][j])).done(function(result){ chunkInBinary = result;console.log(chunkInBinary);} )
 }

Now I want to be able to use the value chunkInBinary outside the done function. Something like this:

for(....){
    $.when(chunkBinary(chunk[i][j])).done(function(result){ chunkInBinary = result;} )
    console.log(chunkInBinary);
 }

Any suggestions on how I can achieve this?

My chunkBinary function returns a promise.

  • 1
    you can't do that if `chunkBinary()` returns a unresolved promise – Arun P Johny Aug 30 '14 at 01:07
  • tell us why do you want to do that then we can try to see whether there is any other solution to the probelem – Arun P Johny Aug 30 '14 at 01:08
  • Well I am resolving it inside the function and when I log it in done function I am getting what I expect to see. All I need is that variables data to be available outside the done function, if possible. –  Aug 30 '14 at 01:09
  • I need to make a service call to post that data to the server. –  Aug 30 '14 at 01:10
  • Before you continue, you need to understand how asynchronous code works. You can't rely on the order of your code when working with asynchronous operations. – m59 Aug 30 '14 at 01:18
  • Isnt done function being called only after the completion of the $.when function? (which has the promise returned to it) ? –  Aug 30 '14 at 01:25

1 Answers1

0

What you have done is already correct.. by creating a variable without the var keyword, u r literally creating a global variable. So now you can access the chuckinbinary variable outside any function as it is in global scope. This is not good practise as u r cluttering ur global work space.

Lakmal Caldera
  • 1,001
  • 2
  • 12
  • 25